[英]How to Reference an instance of an object when “use strict”; has been declared?
I have created an object and instanciated it. 我创建了一个对象并实例化了它。
When I want to access its members (properties, attributes...) an error message is issued. 当我要访问其成员(属性,属性...)时,会发出错误消息。
How to solve the problem ? 如何解决问题?
See the errors on the code as comments. 请参见代码中的错误作为注释。
<script type="text/javascript">
"use strict";
var v = 25;
var dObj = function(){
var v = 100;
var f=(function(){
console.log("Insiding...")
})();
console.log(v); // v locale
console.log(window.v); // v globale.
}
var iObj = dObj();
// THE ERROR LAYS HERE //
// Uncaught TypeError: Cannot read property 'v'
// of undefined at this.html:19 [YANDEX]
console.log(iObj.v);
// And Here too. TypeError:
// iObj is undefined this.html:22:1 [FIREFOX]
iObj.f();
</script>
The var
keyword in Javascript declares a variable; Javascript中的var
关键字声明了一个变量; it does not declare or assign properties of any object. 它不声明或分配任何对象的属性。 It looks like what you wanted to write this: 看起来您想要编写以下内容:
var dObj = function () {
return {
v: 100,
f: function(){
console.log("Insiding...")
},
};
};
When you call a function, for example dObj()
, the value of the function invocation is the "return value" of the function, ie the expression next to the first return
statement inside the function. 当您调用一个函数(例如dObj()
,函数调用的值就是该函数的“返回值”,即函数内第一个return
语句旁边的表达式。 Here, that is an object literal, which has two properties, v
and f
. 在这里,这是一个对象文字,具有两个属性v
和f
。
In your original code, "use strict";
在您的原始代码中, "use strict";
enables strict mode, which is actually not the cause of the error. 启用严格模式,这实际上不是错误的原因。 Then var v = 25;
然后var v = 25;
declares a variable v
, which is actually never used. 声明变量v
,实际上从未使用过。 Then you have the following definition of dObj
: 然后,您具有dObj
的以下定义:
var dObj = function(){
var v = 100;
var f=(function(){
console.log("Insiding...")
})();
console.log(v); // v locale
console.log(window.v); // v globale.
}
This declares a function dObj
that, when called, does the following things. 这将声明一个函数dObj
,该函数被调用时将执行以下操作。 It creates a local variable v
(hiding the v
created earlier) and sets its value to 100. 它创建一个局部变量v
(隐藏先前创建的v
)并将其值设置为100。
Then it creates another local variable f
and sets it equal to the return value of the IIFE ("immediately-invoked function expression") (function() { console.log("Insiding...")})()
- which is undefined
. 然后,它创建另一个局部变量f
并将其设置为等于IIFE(“立即调用的函数表达式”)的返回值(function() { console.log("Insiding...")})()
- undefined
。 This IIFE also outputs "Insiding..." to the console. 该IIFE还向控制台输出“ Insiding ...”。 In other words, that code does not make f
a function. 换句话说,该代码不会使f
功能。 Instead, it evaluates an anonymous function (here outputting "Insiding...") and makes f
its return value (here undefined
). 取而代之的是,它评估一个匿名函数(此处输出“ Insiding ...”)并使其返回值为f
(此处为undefined
)。
Then it outputs the local variable v
, outputting 100. 然后输出局部变量v
,输出100。
Last, it outputs window.v
, which is undefined
. 最后,它输出undefined
window.v
。 (If you had written window.v = 25
earlier, you would be assigning a new property of the window
object, so this would then output 25
instead of undefined
. It is also true that window
is the global object in the browser, so the same effect as window.v = 25
would be achieved if you had not written "use strict"
and had written v = 25
with no var
, but doing that is all kinds of messy, so you're right to use "use strict".) (如果您之前编写了window.v = 25
,则将为window
对象分配一个新属性,因此它将输出25
而不是undefined
。也确实是window
是浏览器中的全局对象,因此如果您没有编写"use strict"
并且没有编写var
而"use strict"
v = 25
,则可以达到与window.v = 25
相同的效果,但是这样做很麻烦,因此使用“ use strict”是正确的。)
var iObj = dObj();
This sets iObj
to the return value of your dObj
function, which is undefined
because there is no return
statement. 这iObj
设置为dObj
函数的返回值,该值undefined
因为没有return
语句。 However, dObj
does run to completion: namely it outputs Insiding...
, 100
, and undefined
. 但是, dObj
确实运行完毕:即输出Insiding...
, 100
和undefined
。
Last, you've got two statements that both throw errors: 最后,您有两个都引发错误的语句:
console.log(iObj.v);
iObj.f();
These both throw errors because iObj
is undefined and has no properties to speak of. 由于iObj
是未定义的并且没有属性可言,因此它们都会引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.