[英]JS this and self don't work
As seen here How to access the correct `this` context inside a callback? 如此处所示, 如何在回调中访问正确的`this`上下文? I try to use self instead of this.
我尝试用自我代替。 It's a little bit a stupid question about JS but I would like some explanations and what should I do to get it right.
关于JS,这是一个愚蠢的问题,但是我想要一些解释,以及应该怎么做才能使它正确。
(function (global) {
"use strict";
var self = this;
function onDeviceReady () {
self.test = "123";
loadMapsApi();
}
function loadMapsApi () {
console.log(self.test);
}
})(window);
And it's not working :) What am I doing wrong ? 而且它不起作用:)我在做什么错? I am using this code inside Cordova btw.. the error is the following
我在Cordova btw中使用此代码。错误如下
Uncaught TypeError: Cannot set property 'test' of undefined
When using strict
mode, the value of this
in a normal function call is undefined
. 使用
strict
模式时,在普通函数调用中this
值的值是undefined
。 That is exactly what your situation is. 那正是你的情况。 Your function:
您的功能:
(function (global) {
"use strict";
var self = this;
...
})(window);
Is just a normal function call so this
will be undefined. 仅仅是一个正常的函数调用所以
this
是不确定的。 If not using strict
mode, then this
in a normal function call will be set to the global object. 如果不使用
strict
模式,那么this
在一个正常的函数调用将被设置为全局对象。 Otherwise, this
gets set to a unique value only when the function is called some other way (with new
, with .apply()
or .call()
or as in obj.method()
). 否则,
this
被设置为唯一的值,只有当函数被调用一些其他的方式(用new
,有.apply()
或.call()
或在obj.method()
The self
work-around you are using is for situations where this
already points at the desired object and you want to save that reference for later use in callbacks. 在
self
变通您使用是的情况下this
已经指着所需的对象,要保存在回调以后使用该引用。 Since that is not the case in your code and it is not clear what you are expecting to use this
for in your code, then it is not clear to use what to recommend to fix your problem without further description of what object you are trying to reference. 由于这是不是在你的代码的情况下,它是不明确你希望使用的
this
在你的代码,那么它是不明确用什么建议没有什么反对你想进一步说明,以解决您的问题参考。
If you just want to reference the global object, then you can just reference global.test
in your code. 如果只想引用全局对象,则只需在代码中引用
global.test
。
(function (global) {
"use strict";
function onDeviceReady () {
global.test = "123";
loadMapsApi();
}
function loadMapsApi () {
console.log(global.test);
}
})(window);
If you are expecting this
to point to some other object, then you will have to explain what you're expecting it to point to and then we can offer you an idea how to reference that specific object. 如果您希望
this
指向其他对象,那么您将必须解释它所指向的对象,然后我们才能为您提供一个如何引用该特定对象的想法。
DO NOT just remove "use strict";
不要只删除
"use strict";
to make things work. 使事情起作用。 The fact that your code doesn't work properly when using
strict
mode means that your code is using a bad practice that strict
mode is designed to protect against. 使用
strict
模式时您的代码无法正常工作的事实意味着您的代码使用了strict
方法来防范strict
模式,这是为了防止这种情况。 Instead, you should continue to use strict
mode and, instead, fix your code to stop using the bad practice and work properly with strict
mode. 相反,您应该继续使用
strict
模式,而应该修复代码以停止使用错误的做法,并在strict
模式下正常工作。
For future reference, if you want to learn how Javascript decides what to set this
to inside a function call, you can read this answer: When you pass 'this' as an argument . 对于未来的参考,如果你想学习JavaScript如何决定该如何设定
this
为函数调用里面,你可以看到这样的回答: 当你通过“这个”作为参数 。 That answer lists the five different ways that the value of this
is determined. 该答案列出了确定
this
值的五种不同方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.