Javascript 理解返回函数中的增量运算符 我试图理解一小段代码 对于第一个i,它打印1,因为++i的值是增量后i的值 但不确定为什么第二个和第三个打印2和3,因为它冲突(值i++是增量之前的i值) 最后,它为什么打印4
你们能告诉我为什么会这样吗Javascript 理解返回函数中的增量运算符 我试图理解一小段代码 对于第一个i,它打印1,因为++i的值是增量后i的值 但不确定为什么第二个和第三个打印2和3,因为它冲突(值i++是增量之前的i值) 最后,它为什么打印4,javascript,Javascript,你们能告诉我为什么会这样吗 //What is i? var i=0; var add = function(){ ++i; console.log(i); //output--->1 return function(){ i++; console.log(i); //output--->2 ///not sure why it happens lik
//What is i?
var i=0;
var add = function(){
++i;
console.log(i); //output--->1
return function(){
i++;
console.log(i); //output--->2
///not sure why it happens like this
return function(){
i++;
console.log(i); //output--->3
///not sure why it happens like this
add();
}
}
};
add()()();
//what does this method do here
- 看来你误解了
++i
和i++
之间的区别。该问题涉及同一语句的运算符优先级
如果您总是在下一行执行console.log()
,您将看不到任何区别
试着这样做:
var x = 0;
console.log(x); // => 0
console.log(++x); // => 1 (increment happened before the console.log)
console.log(x); // => still 1 (nothing changed)
var x = 0; // let's start again
console.log(x); // => 0
console.log(x++); // => still 0 (increment happened after the console.log)
console.log(x); // => 1
现在让我们考虑返回的函数的执行(参见注释):
看起来你误解了
++i
和i++
之间的区别。该问题涉及同一语句的运算符优先级
如果您总是在下一行执行console.log()
,您将看不到任何区别
试着这样做:
var x = 0;
console.log(x); // => 0
console.log(++x); // => 1 (increment happened before the console.log)
console.log(x); // => still 1 (nothing changed)
var x = 0; // let's start again
console.log(x); // => 0
console.log(x++); // => still 0 (increment happened after the console.log)
console.log(x); // => 1
现在让我们考虑返回的函数的执行(参见注释):
添加() 仅通过将i增加1来返回函数 () 执行该函数,以便在增量之后返回另一个函数 () 执行该函数,以便在增量之后有另一个函数,该函数调用add(),该函数再次增量并返回另一个函数 i递增4倍,函数返回函数4倍,但最后一个函数并没有执行,它只是返回到主体 add() ----> function (){ i is 1 now., return ... }
add()() ----> function (){ i is 2 now., return ...}
add()()() ----> function (){ i is 3 now, function(){i is 4 now, return...} }
i++或++i是无关的,除非它位于语句的指令链中,并且如果在同一语句中多次使用它,可能会成为未定义的行为
如果您在最里面的函数中将其称为add(),而不是add(),那么它将是无限递归的
函数中的指令是连续发出的(即使是执行和完成的,除非它们是基于事件的)。add()
仅通过将i增加1来返回函数
()
执行该函数,以便在增量之后返回另一个函数
()
执行该函数,以便在增量之后有另一个函数,该函数调用add(),该函数再次增量并返回另一个函数
i递增4倍,函数返回函数4倍,但最后一个函数并没有执行,它只是返回到主体
add() ----> function (){ i is 1 now., return ... }
add()() ----> function (){ i is 2 now., return ...}
add()()() ----> function (){ i is 3 now, function(){i is 4 now, return...} }
i++或++i是无关的,除非它位于语句的指令链中,并且如果在同一语句中多次使用它,可能会成为未定义的行为
如果您在最里面的函数中将其称为add(),而不是add(),那么它将是无限递归的
函数中的指令是按顺序发出的(即使是执行和完成的,除非它们是基于事件的)。
i
显然在做人们通常期望的事情。我相信您可能对增量前运算符和增量后运算符的区别有点困惑++i
返回i
的递增值,而i++
在递增之前返回i
的原始值。也就是说,它们最终都会递增i
。下面的代码显示了这一点:
var i = 0;
console.log(++i);// prints '1'
console.log(i); // also prints '1'
var j = 0;
console.log(j++);// prints '0' because that was the original value
console.log(j); // prints '1' because j still got incremented in the end
i
显然是在做人们通常期望的事情。我相信您可能对增量前运算符和增量后运算符的区别有点困惑++i
返回i
的递增值,而i++
在递增之前返回i
的原始值。也就是说,它们最终都会递增i
。下面的代码显示了这一点:
var i = 0;
console.log(++i);// prints '1'
console.log(i); // also prints '1'
var j = 0;
console.log(j++);// prints '0' because that was the original value
console.log(j); // prints '1' because j still got incremented in the end
让我们用一些增强的日志来分解它 首先,让我们谈谈大局。您提供的示例代码就是一个咖喱的例子。这是一种函数编程技术,它的意思是,将一个函数分解为一系列函数,其中包含多个参数。你可以,或者只是谷歌一下 这意味着您的函数是一系列函数。我将您的示例更改为一个等效代码,该代码逐个调用每个函数
// The definition of the add function
var add = function(){
console.log('first function in the sequence');
++i;
console.log('i = ', i); //output--->1
return function(){
console.log('second function in the sequence');
i++;
console.log('i =', i); //output--->2
return function(){
console.log('third function in the sequence');
i++;
console.log('i =', i); //output--->3
console.log(typeof(add()));
}
}
};
// This is how you invoke the 'add' sequence one by one
var i=0;
console.log('\n***** calling the sequence one by one \n')
const f2 = add()
console.log(typeof(f2))
const f3 = f2()
console.log(typeof(f3))
f3()
首先我们定义add函数,它与您的函数相同。这实际上是三个函数的顺序
第一个函数是通过调用add()本身来调用的
- 第一个函数将
增加i
。(1
具有初始值 由表示i
var i=0的行定义的
)0的值;
- 然后该函数注销
的值,此时为i
1
- 然后它返回一个函数(第二个函数)
i
的值将为1
为了证明这一点,我在上面的代码中添加了这样的行:
const f2 = add()
console.log(typeof(f2))
此时的控制台输出是
***** calling the sequence one by one
first function in the sequence
i = 1
function
i=1,代码中f2的类型为函数。add()的返回值是一个函数
f3()
让我们调用f2并在这些行上将其返回值赋给f3
const f3 = f2()
console.log(typeof(f3))
这将产生以下输出:
second function in the sequence
i = 2
function
调用了第二个函数并更改了i的值。它返回另一个函数f3。是时候调用第三个函数了
f3()
f3没有返回值,您可以在代码中看到它不返回任何内容。控制台输出如下:
third function in the sequence
i = 3
first function in the sequence
i = 4
function
您可以看到,我们打印出我们在第三个函数中,递增i
,并记录其值,即3
然后,如果你看一下代码,我们会在第三个函数中再次调用add()。因此,第一个函数将再次运行。它增加i并注销其值。
我