Closed Bug 264469 Opened 21 years ago Closed 21 years ago

error or feature in operator 'in' ?

Categories

(Core :: JavaScript Engine, defect)

x86
Windows 2000
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: rusanov, Unassigned)

References

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; rv:1.7.3) Gecko/20040913 Firefox/0.10 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; rv:1.7.3) Gecko/20040913 Firefox/0.10 if execute below script , message box "true - undefined - undefined" operator 'in' find variable 'init' defined in function, this is error or feature? var obj = new Object(); obj.func = function() { var init = 'this init value'; }; alert(('init' in obj.func) + ' - ' + obj.func['init'] + ' - ' + obj.func.init); Reproducible: Always Steps to Reproduce: 1. 2. 3.
*** Bug 264470 has been marked as a duplicate of this bug. ***
This is the correct behaviour. Function variables are considered properties of the Function object (see http://iis1.cps.unizar.es/Oreilly/web/jscript/ch06_03.html, look for "Function arguments and variables are properties"). The "in" operator therefore correctly determines that "init" is a property of the obj.func Function object.
Status: UNCONFIRMED → RESOLVED
Closed: 21 years ago
Resolution: --- → INVALID
I see your documentation I draw attention on string "When the function returns, JavaScript deletes these properties." if property delete why operator 'in' correctly determines that property exist? Function arguments and variables are properties In all versions of JavaScript, global variables are actually properties of some top-level object. In client-side JavaScript, as we'll see, this top-level object is the browser window or frame that contains the JavaScript code. This raises the obvious question: if global variables are properties of an object, what are local function variables? It would make sense that they, too, are properties of some object. The only obvious object is the function (or Function) itself. The following code demonstrates: function f(x) { var y = 3; // a local variable return f.x + f.y; // refer to the argument and variable as properties } If we invoke the function, we see that function arguments and local variables really can be accessed as properties of the function itself: result = f(2); // returns 5 However, if we try to read these properties ourselves, we will be unable to: typeof f.x // yields "undefined" typeof f.y // yields "undefined" What this means is that, like the arguments[] array and the caller property, the local variable and argument properties are only accessible while the function is running. When the function returns, JavaScript deletes these properties.
SpiderMonkey, and its progenitor, the "Mocha" runtime I wrote in a few weeks in May 1995, both have reflected local variable and formal parameter properties in function objects. This is an ECMA extension, arguably allowed by Section 16. I do not want to remove this extension right now, although if you make a sufficiently persuasive case, I could be persuaded. I've made the case myself at the code level: see the XXX comments around http://lxr.mozilla.org/aviarybranch/source/js/src/jsinterp.c#459. But the time is not right to change this early design decision. /be
You need to log in before you can comment on or make changes to this bug.