Global variables with the same name as window properties can't accessed

Assigned to



JavaScript Engine
4 years ago
a year ago


(Reporter: Jason Barnabe (np), Assigned: jorendorff)



28 Branch

Firefox Tracking Flags

(Not tracked)



(1 attachment)



4 years ago
(Outside of any function)

var name = 1;
alert(typeof name);


Firefox 27.0a2 (2013-11-27): alerts "number"
Firefox 28.0a1 (2013-11-27): alerts "string"
Chrome 31.0.1650.57: alerts "string"

Not sure if this was intentional, but it broke my extension Stylish.
This was intentional.  See bug 932322.
Blocks: 932322

Comment 2

4 years ago
OK. This is easy to fix in my code, but I'm wondering how many others will get hit by this. It's certainly a "WTF" result until you realize what's happening.

-Could we get Firefox to warn when declaring a global variable that's already a property of window?
-Can we get this added to "Firefox 28 for developers"?
I flagged bug 932322 as dev-doc-needed; good idea.

For the other, going to defer to the JS folks...

Comment 4

4 years ago
Created attachment 8343537 [details] [diff] [review]

>-Could we get Firefox to warn when declaring a global variable that's already a property of window?

That seems like a good idea. With this patch I get a warning:

    TypeError: redeclaration of global property name

with file and line number pointing to the declaration. I'd be happier if it didn't say TypeError there. Separate bug for the devtools team, I think.

In the shell, it looks like this:

    js> Object.defineProperty(this, "name", {
            get: function() { return "george"; },
            set: function (x) {}});
    js> var name = "fred";
    typein:2:4 warning: redeclaration of global property name
    js> name

What do you think, Jeff? If you like it, I'll figure out how to test it. A new assertWarning() function, combined with indirect eval, or something.
Assignee: nobody → jorendorff
Attachment #8343537 - Flags: feedback?(jwalden+bmo)
Comment on attachment 8343537 [details] [diff] [review]

Review of attachment 8343537 [details] [diff] [review]:

The TypeError bit is an engine bug, really, that we conflate warnings and errors in the same mechanism.  We need to add a separate warning mechanism at some point, to make warnings sane.  Not here, of course.
Attachment #8343537 - Flags: feedback?(jwalden+bmo) → feedback+

Comment 6

a year ago
Is anything else that should be done on this bug, or should we close it?
Well, we could try landing that warning patch....
You need to log in before you can comment on or make changes to this bug.