为什么下面的 javascript 代码会这样运行?
var plusOne = function(num){
return num+1;
};
var total=plusOne(1);
console.log(total);
var total2=plusOne(3);
console.log(total2);
如果我是对的
var total=plusOne(1);
console.log(total);
将值2
返回到变量total
并返回到变量plusOne
,然后将其记录到控制台“2”,但是如果plusOne
的值现在是 2
而不是为什么
var total2=plusOne(3);
console.log(total2);
返回值 4,因为这不是它实际执行的实际代码
var total2=2(3);
console.log(total2);
最佳答案
没有。
Javascript 不能以这种方式工作。事实上,我想不出有任何编程语言能够以这种方式运行。 plusOne
只是一个指向函数的指针。
当您执行第一行时,值 2
存储在 total
中,但 plusOne
根本没有任何反应。
当你执行第二行时,Javascript并不关心该函数是否被称为eariler。
关于javascript - 为什么javascript函数每次都会运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912046/