gpt4 book ai didi

javascript - 为什么javascript函数每次都会运行?

转载 作者:行者123 更新时间:2023-11-28 01:41:09 25 4
gpt4 key购买 nike

为什么下面的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com