instanceof incorrectly returns true for different instances of a closure

RESOLVED DUPLICATE of bug 254067

Status

()

Core
JavaScript Engine
RESOLVED DUPLICATE of bug 254067
13 years ago
13 years ago

People

(Reporter: Karsten Sperling, Unassigned)

Tracking

Trunk
x86
All
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

13 years ago
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
Last Resolved: 13 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.