node.js - Difference between value of 'this' in client-side vs server-side javascript -


writing few nodejs test programs , running few unexpected quirks. in browser when console.log(this); , not in function, window.object. know nodejs has global object when console.log(this) empty object. when ask value of 'this' inside function created undefined. expected reference current function (myclass, in case) going on here?

see following nodejs program:

'use strict'; var log = console.log;   log(this); //empty object  function myclass() {         log (this); //undefined      this.variable = 3; //exception, cannot set property 'test' of undefined }  myclass(); 

thanks

actually, node.js behaves correctly here, because you're not constructing class, calling it's constructor without this context. create new instance of class should use new operator:

new myclass(); 

the difference in behavior caused strict mode, because in strict mode, due security reasons, this is not referencing global object default.


Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -