Hoisting in javascript

夙愿已清 提交于 2020-01-03 19:39:56

问题


I asked a question before and somebody give me a guide and I read it and I saw this

  var temp = setTimeout,
  setTimeout = function() {};

He said that temp will be undefined due to JavaScript hoisting and I dont understand why Its not should be like that?

    var temp;
    temp = setTimeout;
    setTimeout = function() {};

so why its undefined?


回答1:


This is not the same. Your multiple var declaration also declares setTimeout:

var temp = setTimeout,
    setTimeout = function() {};

which is hoisted to

var temp; // = undefined
var setTimeout; // = undefined
temp = setTimeout;
setTimeout = function() {};


来源:https://stackoverflow.com/questions/13505048/hoisting-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!