David G
David G

Reputation: 96810

What is the difference between setting properties to the global object and the window object?

this.f = function() {};
window.d = function() {};

d();
f();

Any difference?

Upvotes: 1

Views: 57

Answers (1)

pimvdb
pimvdb

Reputation: 154828

Not if it's run barely (e.g. not within special functions etc.). Because then this === window.

In constructor functions etc. this has another meaning, so then it matters:

function x() {
    this.a = 123;
}

Now,

  • x() would set window.a to 123
  • var test = new x() would set test.a to 123.

Upvotes: 1

Related Questions