gpt4 book ai didi

javascript - javascript的执行顺序

转载 作者:行者123 更新时间:2023-11-30 05:52:43 25 4
gpt4 key购买 nike

我读过

JavaScript caches declared functions before any other variables, afterthis, it goes back to the top of the scope and runs variabledefinitions and functions calls in the order that they appear

我不明白这个例子

//bob first initialization
function bob()
{
alert('bob');
}

//set jan to bob via reference
var jan = bob;

//set bob to another function
function bob()
{
alert('newbob');
}

jan(); //alerts 'bob'
bob(); //alerts 'newbob'

两个 bob() 函数在执行前都被声明和缓存。为什么 jan() 会提醒“bob”而不是“newbob”?当 jan 被初始化时, bob() 已经被重新声明。

有什么想法吗?谢谢

最佳答案

因为 jan 指向 bob 的第一个声明(作为指针)而不是新声明的 bob 你需要设置 jan = bob; 在第二次声明之后

虽然不太确定。

关于javascript - javascript的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13726711/

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