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
Post a Comment