The 'this' keyword behaves differently in Nodejs and browser

前端 未结 6 1886
栀梦
栀梦 2020-12-01 10:36

I have this piece of code :

var obj1;
var obj2;

function x() {
    obj1 = this;
}

function y() {
    obj2 = this;
}

x();
y();

console.log(obj1 === o         


        
6条回答
  •  误落风尘
    2020-12-01 11:11

    Created a table to show the this value under different scopes and modes:

    Note: In Node environment, the value of this outside the function means that this value is outside of a function declaration but within the anonymous function wrapper created by node.js for achieving the module scope.

提交回复
热议问题