The 'this' keyword behaves differently in Nodejs and browser

前端 未结 6 1887
栀梦
栀梦 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:19

    In the Browser-Context the last this points to the Windowobject, which does exist in Node-Context. Therefore the last this is an empty object. The occurrences of this in the functions however point to some global object in the Node-Context.

提交回复
热议问题