我刚安装了Node v7.2.0并了解到以下代码:
var prm = Promise.reject(new Error('fail'));
Run Code Online (Sandbox Code Playgroud)
结果如下:
(node:4786) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: fail
(node:4786) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Run Code Online (Sandbox Code Playgroud)
我理解这背后的原因,因为许多程序员可能已经经历了Error最终被一个人吞没的挫败感Promise.然而,我做了这个实验:
var prm = Promise.reject(new Error('fail'));
setTimeout(() => {
prm.catch((err) => {
console.log(err.message);
})
},
0)
Run Code Online (Sandbox Code Playgroud)
这导致:
(node:4860) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: fail
(node:4860) DeprecationWarning: Unhandled promise rejections …Run Code Online (Sandbox Code Playgroud) 使用Webstorm 11.0.3和Lodash 4.3.0.我已经安装了loadash DefinitelyTyped(见底部的图片),但仍然没有得到自动完成:
_.has
Run Code Online (Sandbox Code Playgroud)
除了下载和安装DefinitelyTyped全局包之外,我还需要做任何其他事情吗?
TIA!
背景说明
我问了一个关于使用循环定义日期数组的问题.
该数组是根据名为"dateinterval"的声明变量定义的.我设计代码的方式导致了与另一个循环相关的错误消息,另一个用户为我提供了另一个解决此问题的循环.
现在我已经仔细比较了两种不同的解决方案,我根本不明白为什么它们不会产生相同的结果.
我的代码
我开发了以下代码来定义UTC格式的日期数组.然而,结果是自1970年1月1日00:00:00以来以毫秒为单位的日期数组.换句话说就是一个数字.
for (var i=0; i < difference; i++){
dateinterval[dateinterval.length] = dateinterval[0].setDate(datointerval[0].getDate() + i);
};
Run Code Online (Sandbox Code Playgroud)
适当的解决方案
下面的代码是另一个用户提供给我的正确代码(再次感谢!)此代码定义了一个UTC日期数组.
for (var i = 0; i < difference; i++) {
var dt = new Date(dateinterval[0]);
dt.setDate(dt.getDate() + i);
dateinterval[dateinterval.length] = dt;
};
Run Code Online (Sandbox Code Playgroud)
我不明白
我几乎失明了,因为他们盯着两种不同的解决方案来弄清楚差异是什么,我只是不明白.
对于未经训练的人来说,似乎两段代码执行完全相同的操作,唯一的区别在于它们的结构.我被告知setDate返回millisecs,在我的代码中,这些毫秒被分配给数组.但在适当的解决方案中,变量DT也被赋予一个setDate值 - 据我所知 - 它也应该是毫秒.那么为什么这条线:
dateinterval[dateinterval.length] = dt;
Run Code Online (Sandbox Code Playgroud)
不将millisecs分配给dateinterval数组?
任何人都可以向我解释这一点,以便我可以更好地理解 Javascript而不仅仅是复制工作解决方案吗?
背景资料
我正在设置一个函数,它根据开始日期和结束日期创建日期数组.
该函数将接收开始日期和结束日期,这些日期首先被格式化为year-month-dayT12:00:00:00格式化,然后使用.getTime()格式转换为毫秒.
我的剧本
我已经创建了以下脚本来创建数组.
var $date_array = [];
function calc_workdays_between_dates (a, b) {
function $create_date_array ($start_date, $end_date) {
var $counter = 0;
while ($start_date !== $end_date) {
var x = new Date($start_date);
x.setDate(x.getDate() + $counter);
$date_array.push(x);
$start_date = x.getTime();
$counter++;
}
}
$create_date_array (a, b);
}
Run Code Online (Sandbox Code Playgroud)
请注意,在$create_date_array函数内部嵌套函数是有原因的$calc_workdays_between_dates.现在我已经删除了$calc_workdays_between_dates函数的所有其他部分,只关注手头的问题(我也在这个剥离版本上运行我的测试 - 所以函数的其余部分不会影响任何东西).
我的问题
例1:
如果我调用函数在calc_workdays_between_dates (x1, x2);哪里:
x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function
x2 …Run Code Online (Sandbox Code Playgroud) 背景
我正在尝试如何Generator.prototype.throw()工作并制作了这个例子:
var myGen = function *() {
try{
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
}
catch(err) {
console.log(err);
}
yield 7;
yield 8;
yield 9;
}
var myIterator = myGen();
console.log(myIterator.next());
console.log(myIterator.next());
console.log(myIterator.next());
myIterator.throw(new Error('Bullocks!'));
console.log(myIterator.next());
console.log(myIterator.next());
console.log(myIterator.next());
Run Code Online (Sandbox Code Playgroud)
在运行时导致以下结果:
{ value: 1, done: false }
{ value: 2, done: false }
{ value: 3, done: false }
[Error: Bullocks!]
{ value: 8, done: false }
{ value: 9, done: false }
{ value: …Run Code Online (Sandbox Code Playgroud) 代码
我编写以下代码并将其另存为 test.js:
var foo = 'I am local';
global.foo = 'I am global';
function print () {
console.log(this.foo);
};
print();
console.log (this.foo);
Run Code Online (Sandbox Code Playgroud)
然后我使用命令在终端中运行它node test.js,它返回:
I am global
undefined
Run Code Online (Sandbox Code Playgroud)
问题
为什么不返回:
I am global
I am global
Run Code Online (Sandbox Code Playgroud)
?
背景
我想要一个跟踪自己状态的函数:
var myObject = {
myFunction: function () {
var myself = this.myFunction;
var firstTime = Boolean(!myself.lastRetry);
if (firstTime) {
myself.lastRetry = Date.now();
return true;
}
// some more code
}
}
Run Code Online (Sandbox Code Playgroud)
上述代码的问题在于,值this将取决于函数调用的站点.我希望函数能够在不使用的情况下引用自身:
myObject.myFunction.bind().apply().call()题
是否有可能提供一种独立于其呼叫站点的这种自我意识的功能,并且没有外部参考的任何帮助?
javascript ×6
node.js ×2
arrays ×1
counter ×1
date ×1
ecmascript-6 ×1
es6-promise ×1
function ×1
generator ×1
lodash ×1
loops ×1
oauth-2.0 ×1
podio ×1
this ×1
webstorm ×1
while-loop ×1