Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save greggman/15b5285fab6e0ca0a799 to your computer and use it in GitHub Desktop.

Select an option

Save greggman/15b5285fab6e0ca0a799 to your computer and use it in GitHub Desktop.

Revisions

  1. greggman revised this gist Apr 7, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion use-strict.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ The same would be true of adding properties to elements. If you've got code like
    function makeDivWithProperities(parent) {
    var div = document.createElement("div");
    parent.appendChild(div);
    div.deviceWidth = div.clientWidth * window.devicePixelRatio; // Scary!
    div.deviceWidth = someMagicFunctionToComputeDeviceWidth(div); // !!!
    }

    If tomorrow some standard decides to add `deviceWidth` to all HTMLElements than any app using that name will break.
  2. greggman revised this gist Apr 7, 2015. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions use-strict.md
    Original file line number Diff line number Diff line change
    @@ -13,9 +13,21 @@ get added to the window object. I can't believe I've been programming web stuff
    But.... The bigger issue is this seems like a timebomb waiting to happen. The problem is you write some code today. Let's say you write

    "use strict";
    var cameraResolution = 1000;
    var numProcessesAvailable = 100; // Some application specific variable.

    Tomorrow some new web standard comes out and decides there should be a variable on window called `cameraResolution`. At that point every program what was using a variable in the global scope called `cameraResolution` breaks.
    Tomorrow some new web standard comes out and decides there should be a variable on window called `numProcessesAvailable`. At that point every program what was using a variable in the global scope called `numProcessesAvailable` breaks.

    The same would be true of adding properties to elements. If you've got code like this

    "use strict"

    function makeDivWithProperities(parent) {
    var div = document.createElement("div");
    parent.appendChild(div);
    div.deviceWidth = div.clientWidth * window.devicePixelRatio; // Scary!
    }

    If tomorrow some standard decides to add `deviceWidth` to all HTMLElements than any app using that name will break.

    Can that really be what Firefox wants? Is there an issue filed about this somewhere? I'd like to read the thinking on this issue. It seems like it has to be the Chrome way otherwise every time a new property is added to any native browser object and you're running in strict mode you run the risk of breaking lots of web pages.

  3. greggman revised this gist Apr 7, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions use-strict.md
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ get added to the window object. I can't believe I've been programming web stuff

    But.... The bigger issue is this seems like a timebomb waiting to happen. The problem is you write some code today. Let's say you write

    "use strict";
    var cameraResolution = 1000;
    "use strict";
    var cameraResolution = 1000;

    Tomorrow some new web standard comes out and decides there should be a variable on window called `cameraResolution`. At that point every program what was using a variable in the global scope called `cameraResolution` breaks.

  4. greggman created this gist Apr 7, 2015.
    22 changes: 22 additions & 0 deletions use-strict.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    So I had a sample that wasn't working in firefox. It had code like ths

    "use strict";
    var devicePixelRation = window.devicePixelRatio || 1;

    That worked fine on Chrome but broke on Firefox with

    TypeError: setting a property that has only a getter

    So first thing I learned something I didn't know. Variables declared **with** `var` in the global scope
    get added to the window object. I can't believe I've been programming web stuff for like forever and even was on the Chrome team for 5 years and didn't know this. I new variables without `var` were added to the window but I didn't know globals **with** `var` were added. They aren't added to other objects in other scopes. I'm going to have to assume this is some legacy issue they can't get rid of

    But.... The bigger issue is this seems like a timebomb waiting to happen. The problem is you write some code today. Let's say you write

    "use strict";
    var cameraResolution = 1000;

    Tomorrow some new web standard comes out and decides there should be a variable on window called `cameraResolution`. At that point every program what was using a variable in the global scope called `cameraResolution` breaks.

    Can that really be what Firefox wants? Is there an issue filed about this somewhere? I'd like to read the thinking on this issue. It seems like it has to be the Chrome way otherwise every time a new property is added to any native browser object and you're running in strict mode you run the risk of breaking lots of web pages.

    No? Thoughts?