espruino / Espruino

The Espruino JavaScript interpreter - Official Repo

Home Page:http://www.espruino.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Constructor comparisons don't work

RaphiSpoerri opened this issue · comments

Regarding primitive types, comparisons like <T literal>.constructor == <T>.prototype.constructor do not always give consistent results. For example

const cmp = () => "blah".constructor == String.prototype.constructor;
console.log(
    cmp() == cmp()
);

prints

>false

(I'm using the web IDE's Bangle.js 2 emulator, and version 2v18.1)

Thanks - interestingly, "blah".constructor seems to return undefined initially, but if you call String.prototype.constructor it's fine afterwards - so it seems to be having trouble creating the constructor.

So if you change the order of comparison, it appears to work reliably for me? Is that the same for you?

const cmp = () => String.prototype.constructor == "blah".constructor;
console.log(
    cmp() == cmp()
);

I've got a fix for this, but it appears to break some other stuff so I'm going to hold off committing it.

I believe the 'real prototype chain' branch would fix this too.