Closed Bug 274122 Opened 20 years ago Closed 20 years ago

instanceof incorrectly returns true for different instances of a closure

Categories

(Core :: JavaScript Engine, defect)

x86
All
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 254067

People

(Reporter: spiff, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10

This code demonstrates the problem:

 function makecls(name) {
   return function() { this.clsname = name };
 }
 A = makecls('a'); a = new A();
 B = makecls('b'); b = new B();
 if (a instanceof B) alert('BUG!');

A and B are different function objects, and neither of their prototypes is an
instance of the other constructor, so a should not be an instance of B.

The ECMA-Script standard says that instanceof is to be implemented via a
HasInstance function, which should in this case take the prototype property of B
and compare it to each object in the prototype chain of a.

However, B (correctly) is not on that prototype chain, as can be easily
demonstrated with
 B.prototype.fun = 42;
 alert(a.fun); // undefined
 
Hence, the instanceof operator seems to be the culprit. Note that isPrototypeOf
works correctly:
 alert(B.prototype.isPrototypeOf(a)); // false


Reproducible: Always
Steps to Reproduce:
see Details
Actual Results:  
see Details

Expected Results:  
see Details
Works for me in a trunk js shell:

$ Linux_All_DBG.OBJ/js
js> alert=print
 
function print() {
    [native code]
}
 
js> function makecls(name) {
   return function() { this.clsname = name };
 }
js>  A = makecls('a'); a = new A();
[object Object]
js>  B = makecls('b'); b = new B();
[object Object]
js>  if (a instanceof B) alert('BUG!');
js> a instanceof A
true
js> a instanceof B
false
js> b instanceof A
false
js> b instanceof B
true
js>

You are using an older version of Gecko than the trunk version.  This looks like
a dup of bug 254067.  If you want that bug fixed in 1.7.6 and any other branch
releases, please nominate it by setting the blocking1.7.6 flag to [?].

/be

*** This bug has been marked as a duplicate of 254067 ***
Status: UNCONFIRMED → RESOLVED
Closed: 20 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.