Closed
Description
The code example below should log the numbers from 0 up to 500. Instead, the code breaks after about 100 iterations. The issue is that after about 100 iterations the constructor of class B
returns the class itself instead of an instance of B
.
The fact that this issue only occurs after multiple iterations suggests to me that the bug is somewhere in the JIT engine.
By the way, the error disappears if the method body of A.toB()
is replaced by return new B(this);
.
I tested the following code in Microsoft Edge 38.14393.0.0 (Microsoft EdgeHTML 14.14393)
var Test = {};
class A {
constructor(foo) { this.foo = foo; }
toB() { return new Test.B(this); }
}
class B {
constructor(bar) { this.bar = bar; }
}
Test.B = B;
for (let i=0; i<500; i++)
{
const a = new A(i);
const b = a.toB();
try
{
console.log(b.bar.foo);
}
catch (e)
{
console.log(e); // e.description -> "Unable to get property 'foo' of undefined or null reference"
console.log(b); // b -> class B { ... }
break;
}
}