Search code examples
javascriptwebstorm

Why this.bar returns a different result on webstorm ?


I am learning Javascript and I am currently watching a lecture about it. One of the example in lecture like this;

function foo(){
    console.log(this.bar);
}

var bar = "bar1";
var o2 = { bar: "bar2", foo: foo};
var o3 = { bar: "bar3", foo: foo};

foo();     // bar1
o2.foo();  // bar2
o3.foo();  // bar3

This is work as intended in the comments when I try to run on Google Chrome console. But I am working on Webstorm and I realized foo(); returns undefined. It is probably foo(); doesn't called from global but why this is working like this on Webstorm. ECMAscript is 5.1 and I changed and tried on different versions but the result was same. Thank you for any help.

Webstorm console result;

undefined
bar2
bar3

Solution

  • If you're not executing client-side JS, WebStorm is using node.js. The behaviour of that code is explained here.