18
var test = function() {

    'use strict';

    var mapNames = {
        'name': 'City Name:',
        'coord.lat': 'Latitute:'
    };  

    for (var key in mapNames) {

        var names;

        if (mapNames[key]) {
            name = mapNames[key];
        } else {
            name = key;
        }
    }

    console.log(name);

}

test();

In the code above I made a mistake by declaring variable names and using name instead. I thought 'strict' mode would catch it but it didn't. Shouldn't this throw an error in this case?

Boann
  • 48,794
  • 16
  • 117
  • 146
JS-JMS-WEB
  • 2,555
  • 3
  • 17
  • 26

1 Answers1

25

A name global variable already exists, unrelated to your code; it represents the name of the current window, so you are assigning to an already existing variable.

window.name; // the name of the current window for cross-window communication

Everything on window is declared as a global - so it is not reference-erroring since it is assigning to a variable in an outer scope.

Super confusing :D


"use strict" would prevent defining new global variables, here we are performing an assignment to an existing variable, think of it as name is in the global scope, like window.Blob, window.console and so on.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 1
    shouldn't `"use strict"` prevent exactly that? – John Dvorak Jun 28 '15 at 11:28
  • 3
    @JanDvorak `"use strict"` would prevent defining new global variables, here we are performing an assignment to _an existing variable_, think of it as `name` is in the global scope, like `window.Blob`, `window.console` and so on. – Benjamin Gruenbaum Jun 28 '15 at 11:29
  • ah, browser already provides a variable with that name. Can we have a super-strict mode where globals can't be accessed by name? :-) – John Dvorak Jun 28 '15 at 11:31
  • @JanDvorak I don't think so that there would be any way. – Bhojendra Rauniyar Jun 28 '15 at 11:32
  • `typeof document.name` is always `'string'` – Nina Scholz Jun 28 '15 at 11:32
  • It's not only globals, it's the outer scope (recursively) where you defined (in this case) your function. You need an extra language feature to explicitly tell that you want to access an outer scope.. (e.g. `global` in php and python) – Karoly Horvath Jun 28 '15 at 11:33
  • 1
    @KarolyHorvath The point is that even if you don't define a variable called `name`, the browser does that for you in the global scope. – John Dvorak Jun 28 '15 at 11:36
  • 1
    Also note that the strict mode catches the error just fine in node (because there's no "window"). – georg Jun 28 '15 at 11:37
  • 1
    @georg yes, strict mode errors (as it should) in any environemt where the host object does not define a `name` property. – Benjamin Gruenbaum Jun 28 '15 at 11:37
  • BTW, it might be clearer if *`name` global variable* is changed to *`name` global **property***. This merging of variables and properties only happens for the global object/environment record. – RobG Jun 30 '15 at 02:11