1

Here's the code, it's pretty simple:

(function() {
  "use strict";

  // Define our constructor
  this.White = function() {
    this.version = "1.0.0";
  };
}());

// Later
a = new White();
alert(a.version);

In JSBin (and when running JShint), it works like it should. Doesn't work in JSBin after adding "use strict". When running this script in Chrome, however, I get this vague message:

enter image description here

Uncaught TypeError: Cannot set property 'White' of undefined

Why?!

FloatingRock
  • 6,741
  • 6
  • 42
  • 75
  • 5
    Because of `use strict` `this` is `undefined`, othrwise it'd be the global object. – elclanrs Jan 15 '15 at 19:49
  • 2
    Your code is expecting `this` to be a reference to the global object. In your code (outside jsbin), if the outer scope is in "strict" mode, then `this` will be `undefined` (because that's how "strict" mode works). – Pointy Jan 15 '15 at 19:49
  • @elclanrs thanks, that does make sense! So I just need to do `window.White` instead of `this.White` correct? – FloatingRock Jan 15 '15 at 19:54
  • 1
    Note that the JSBin appears to have been saved without the `"use strict";` in the snippet you posted. – Jonathan Lonowski Jan 15 '15 at 19:54
  • @JonathanLonowski my bad, thanks for pointing that out. Adding `'use strict';` to JSBin breaks it too .. updating the title. – FloatingRock Jan 15 '15 at 19:55

1 Answers1

2

using strict mode will not let you create implicit globals.this.White is hoisted globally and it is implicitly created.So removing it will remove the error Strict Mode

AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • 2
    If you'd rather not disabled strict mode, you can simple replace `this.White = ...` with `window.White = ...`, for browser based JS. – FloatingRock Jan 16 '15 at 06:56