简体   繁体   English

当“使用严格”时如何引用对象的实例; 已经申报了吗?

[英]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 . 在这里,这是一个对象文字,具有两个属性vf


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...100undefined

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.

相关问题 如何在没有严格模式的情况下执行 deno? 错误:未捕获的 SyntaxError:标识符“列表”已被声明 - How to execute deno without strict mode ? Error: Uncaught SyntaxError: Identifier 'list' has already been declared 当 object 尚未在 vue 中声明时,如何处理对象的输入? - how to handle input for objects when the object has not yet been declared in vue? Javascript:当“this”被“call”覆盖时,如何获取对父对象的引用? - Javascript: How to get a reference to the parent object when “this” has been overwritten with “call”? 如何确定是否已声明Javascript类? - How to determine if a Javascript class has been declared? 功能尚未声明 - Function has not been declared 将jquery-ui自动完成功能所属的对象/ dom元素应用于所有输入后,如何引用它 - How do I reference the sepcific object/dom element that an jquery-ui autocomplete function belongs to, when it has been applied to all inputs 在函数声明后使用变量(在函数中) - Use variable (in function) after function has been declared javascript + svg:在样式之外声明时填充 - javascript + svg : get fill when it has been declared outside of a style 为什么 google 变量从未被声明时存在? - Why does google variable exist when it has never been declared? 如何使用在另一个文件中定义的JavaScript方法/对象? - How to use a JavaScript method/object that has been defined in another file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM