In the following sample, "function foo()" statement does not call the foo
setter and what's even stranger, after it, "window.foo()" invokes the getter
but just "foo()" does not and instead calls the statement defined foo
function.
If the function statement is replaced by a function expression "var foo =
function() ..." then it bahves as expected, same as Chrome and Opera.
<html>
<head>
<script>
__defineGetter__('foo', function() {alert('foo getter'); return function()
{alert('new foo');};});
__defineSetter__('foo', function() {alert('foo setter');});
</script>
<script>
function foo()
{
alert('statement foo');
}
</script>
</head>
<body>
<button onclick="foo();">foo()</button><br>
<button onclick="window.foo();">window.foo()</button>
</body>
</html>
Here's what the standard (10.1.3) has to say about this:
"For each FunctionDeclaration in the code, in source text order, create a
property of the variable object whose name is the Identifier in the
FunctionDeclaration, whose value is the result returned by creating a
Function object as described in section 13, and whose attributes are
determined by the type of code. If the variable object already has a
property with this name, replace its value and attributes. Semantically,
this step must follow the creation of FormalParameterList properties."
So the value of the existing property is to be replaced. That appears to be
consistent with calling the setter with the new value, as Chrome and Opera
are doing.
Is this not the right group for the above? Can someone from Mozilla please
comment?
This moved to es5-discuss:
https://mail.mozilla.org/pipermail/es5-discuss/2010-July/thread.html
as well as the bug you filed:
https://bugzilla.mozilla.org/show_bug.cgi?id=577325
The es5-discuss thread is settling on a change to ES5 for ISO
standardization, if I'm recalling and following correctly. The spec
change should then lead to a code change for the SpiderMonkey bug. All
very soon (in time for ISO standardization of ES5, and for Firefox
4!).
/be