1

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
Alper Silistre
  • 117
  • 2
  • 12
  • In first case in browser, this refers to window and as `var bar` is defined in global context, it belongs to `window` this might not be the case in your IDE.. – Rayon Dec 25 '15 at 17:17
  • `foo` never returns anything but `undefined`. There is no `return` statement. Did you mean to say Chrome prints to the console and Webstorm does not? – zvone Dec 25 '15 at 17:23
  • @zvone No it does not relevant with console. I learned from answer that node.js files has own modules so var bar = .. etc. belongs to local module scope. Thats why I am taking different results. – Alper Silistre Dec 25 '15 at 22:28

1 Answers1

0

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

Community
  • 1
  • 1
Ilya
  • 5,377
  • 2
  • 18
  • 33