Open Bug 943958 Opened 11 years ago Updated 2 years ago

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

Categories

(Core :: JavaScript Engine, defect)

28 Branch
x86_64
Linux
defect

Tracking

()

People

(Reporter: jason.barnabe, Unassigned)

References

Details

(Keywords: regression)

Attachments

(1 file)

(Outside of any function)

var name = 1;
alert(typeof name);

Results:

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
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...
>-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
    "george"

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]
bug-943958-warn-redecl-v1.patch

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+
Is anything else that should be done on this bug, or should we close it?
Well, we could try landing that warning patch....

The bug assignee didn't login in Bugzilla in the last 7 months, so the assignee is being reset.

Assignee: jorendorff → nobody
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: