Closed Bug 281930 Opened 21 years ago Closed 21 years ago

|this| is set to wrong value inside function expression

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

VERIFIED FIXED
mozilla1.8beta2

People

(Reporter: jerfa, Assigned: brendan)

Details

(Whiteboard: [have patch])

Attachments

(3 files, 1 obsolete file)

After executing the following line, var rv = function() { return this; }(); rv should be a reference to the global object. It is a reference to the anonymous function instead.
Expected output: true [object Window] Actual output: false function () { return this; }
Attached file corrected test case (obsolete) —
you are testing a reference to the function against windows and not the return value of calling the function. INVALID.
Please look at my testcase again: I'm calling the function but I don't get the expected return value. This works as I expect in IE6, Opera, Netscape 4.75 and Rhino.
Sorry, I missed the trailing () call on the function expression. var rv = function() { return this; }(); sets rv to the function rather than the result of calling the expression. var rv = (function() { return this; })(); will work. The bug isn't about |this| being wrong but is about the () call being ignored.
Attachment #174041 - Attachment is obsolete: true
Sorry, but that's not right either. If you modify the line to var rv = function() { alert("Yo"); return this; }(); you'll see the function gets called properly.
you are right. If you change the alert("Yo") to alert(this) you see that |this| is the function expression.
Attached patch fixSplinter Review
I'm going to check this in when the trunk opens; shaver, feel free to stamp r= any time. /be
Attachment #174076 - Flags: review?(shaver)
Assignee: general → brendan
Priority: -- → P3
Target Milestone: --- → mozilla1.8beta2
Comment on attachment 174076 [details] [diff] [review] fix r=shaver
Attachment #174076 - Flags: review?(shaver) → review+
Status: NEW → ASSIGNED
Whiteboard: [have patch]
Trunk's open...
So it is, thanks for the nudge. /be
Status: ASSIGNED → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
Verified with build 20050223, thanks!
Status: RESOLVED → VERIFIED
js/tests/js1_5/Regress/regress-281930.js checked in with Erik's permission.
Flags: testcase+
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: