[英]Why is this.defaultNumber undefined? Trying to understand IIFE
Given this code: 鉴于此代码:
const numberManipulator = (function() {
this.defaultNumber = 5;
const doubleNumber = function(num) {
console.log(this.defaultNumber);
return num !== undefined ? num * 2 : new Error('no number provided!');
}
return {
doubleNumber
}
})();
const test = numberManipulator.doubleNumber(20);
Why is console.log(this.defaultNumber
showing undefined
but if I console.log(defaultNumber
, it shows 5
? I thought that it would show undefined for the latter. 为什么是
console.log(this.defaultNumber
显示undefined
但是如果我是console.log(defaultNumber
,它显示5
?我认为它会显示未定义的后者。
Thanks 谢谢
You should read How does the “this” keyword work? 您应该阅读“this”关键字如何工作?
When the IIFE is called, its this is not set so it defaults to the global object, so in: 当IIFE被调用时,它这是不是设置成默认为全局对象,所以在:
const numberManipulator = (function() {
this.defaultNumber = 5;
...
})();
this.defaultNumber = 5
creates a property of the global (window in a browser) object called defaultNumber and assigns it a value of 5. this.defaultNumber = 5
创建一个名为defaultNumber的全局(浏览器中的窗口)对象的属性,并为其赋值5。
If the return statement should really be: 如果return语句应该是:
return {doubleNumber: doubleNumber}
then the IIFE returns an object reference to numberManipulator . 然后IIFE返回对象引用numberManipulator。 When called as:
当被称为:
numberManipulator.doubleNumber(20)
then this within doubleNumber is numberManipulator , which doesn't have a defaultNumber property so this.defaultNumber
returns undefined . 那么doubleNumber中的 这个是numberManipulator ,它没有defaultNumber属性,所以
this.defaultNumber
返回undefined 。
Long story short: 长话短说:
1. The IIFE is executing in the context of the window
I was wrong, see RobG's comment.1. IIFE正在我错误的 window
执行,请参阅RobG的评论。
2. this
thus refers to the window
inside the IIFE, since it is not set by default. 2.
this
从而指的是window
的内IIFE,因为它不是默认设置。
3. Your doing this.defaultNumber = 5;
你这样做
this.defaultNumber = 5;
is hence equivalent to 因此相当于
window.defaultNumber = 5;
4. The second method executes in the context of numberManipulator
and so logging this.defaultNumber
is equivalent to numberManipulator.defaultNumber
which was never set by you. 4.第二种方法在
numberManipulator
的上下文中numberManipulator
,因此记录this.defaultNumber
等同于您从未设置的numberManipulator.defaultNumber
。 Hence, it is undefined. 因此,它是未定义的。
Explanation on this
: this
解释:
Inside any function, the this
keyword refers to the current context in which the function is executing. 在任何函数内部,
this
关键字引用函数执行的当前上下文 。 It doesn't matter where you defined that function inside your codebase. 在代码库中定义该函数的位置并不重要。 All it matters in what context are you calling it.
在你称之为什么背景下,这一切都很重要。 In your current case, you're calling the IIFE in the context of the window.
在您当前的情况下,您在窗口的上下文中调用IIFE。 Doing:
这样做:
const numberManipulator = (function() {
// MAGIC
})();
amounts to: 总数是:
window
context. window
上下文中运行IIFE。 However, the context of a function may be changed by using .call
, .apply
, or by simply calling that method as a property of another object. 然而,功能的上下文中可以使用被改变
.call
, .apply
,或通过简单地调用该方法作为另一对象的属性。 In your second log, this.defaultNumber
, this
refers to the object since you called the method using numberManipulator.doubleNumber(20);
在你的第二个日志
this.defaultNumber
, this
是指你使用numberManipulator.doubleNumber(20);
调用方法后的对象numberManipulator.doubleNumber(20);
. 。 Calling methods using
object.prop()
sets their context ( =this
) to that object
. 使用
object.prop()
调用方法将其上下文( =this
)设置为该object
。
Hope that helps! 希望有所帮助! :D
:d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.