如果我有一个 IIFE,这是否指的是本地范围?
(function(){
var $a;
$a = Su.$a
// this.$a = Su.$a; // can I replace with this
})();
我问是因为我需要 Su.$a
在我的 IIFE 中随处可用。
但是我不想调用Su.$a
,我想调用$a
。
是说 this.$a
当 var 是顶级作用域时说 var $a
一样吗?
最佳答案
没有。
this
由一些东西设置,由 MDN / this
Operator 描述,但简而言之:
- 全局对象,在顶级范围
obj
,当执行obj.func(...)
obj
,当执行func.apply(obj, [...])
或func.call(obj, ...)
或全局对象,如果obj
是null
或undefined
- 在调用
new func(...)
时,一个带有原型(prototype)func.prototype
的新对象 - 事件目标,如果
elem.addEventListener('event', func, ...)
和event
在elem
上触发/li>
较新的 JavaScript 中有一些差异和新增内容,但仅此而已。 this
与 function
作用域无关。
关于javascript - IIFE 中的 'this' |与本地范围相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12151226/