I have a JavaScript file which is loaded by require
.
// loaded by require()
var a = this; // "this" is an empty object
this.anObject = {name:"An object"};
var aFunction = function() {
var innerThis = this; // "this" is node global object
};
aFunction();
(function(anyParameter){
console.log(anyParameter.anObject);
})(
this // "this" is same having anObject. Not "global"
);
My question is: this
in var a = this;
is an empty object whereas this
statements in functions are shadows of node.js global object. I know this
keyword is different in functions but I could not understand why first this
is not equal to global and this
in functions equals to global.
How does node.js inject global
to this
in function scopes, and why it does not inject it to the module scope?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…