Closed Bug 366864 Opened 18 years ago Closed 18 years ago

If "apply" and "call" methods of Function are used, and a scopeObject is supplied, then the JavaScript engine calls "valueOf", which causes a problem if you have overridden it on the scopeObject.

Categories

(Core :: JavaScript Engine, defect)

x86
Windows 2000
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 314874

People

(Reporter: juliant, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8) Gecko/20051111 Firefox/1.5
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8) Gecko/20051111 Firefox/1.5

The Function object has "apply" and "call" methods.  The problem affects both.

The "apply" method looks like this:- 

apply(thisArg[, argArray])

By supplying "thisArg" the function is applied as a method of "thisArg".

When it does so, the JavaScript engine calls "valueOf".

If "valueOf" has been overridden on the object represented by "thisArg", then the JavaScript engine calls the overridden method.

Result :

If  the overridden valueOf() returns a different Object or Array to "thisArg", then the "thisArg" is treated as the Object or Array returned by the overridden "valueOf"

If the overridden valueOf() returns a String or Number, then the "thisArg" is treated as the object originally supplied as "thisArg".

This behaviour is different to IE and Opera.


Reproducible: Always

Steps to Reproduce:
Run the following code:-

<html>
<head>
<script>

function MyObject()
{
	this.prop = ["a"]; // Error!
	// this.prop = "Hi"; // No error!
}

MyObject.prototype.valueOf = function()
{
	return this.prop;
}

function testMethod()
{
	alert(this.constructor);
	alert(this.prop);
}

var instance = new MyObject();
testMethod.apply(instance);

</script>
</head>
<body>
</body>
</html>
Actual Results:  
Alerts "function Array (){[native code]}"
Alerts "undefined"

Expected Results:  
Alerts "function MyObject..."
Alerts "a"
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.