简体   繁体   中英

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; 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. Here, that is an object literal, which has two properties, v and f .


In your original code, "use strict"; enables strict mode, which is actually not the cause of the error. Then var v = 25; declares a variable v , which is actually never used. Then you have the following definition of 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. It creates a local variable v (hiding the v created earlier) and sets its value to 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 . This IIFE also outputs "Insiding..." to the console. In other words, that code does not make f a function. Instead, it evaluates an anonymous function (here outputting "Insiding...") and makes f its return value (here undefined ).

Then it outputs the local variable v , outputting 100.

Last, it outputs window.v , which is undefined . (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".)

var iObj = dObj();

This sets iObj to the return value of your dObj function, which is undefined because there is no return statement. However, dObj does run to completion: namely it outputs Insiding... , 100 , and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM