Skip to content

JS Type System vs CLR Type System (inheritance behavior) #284

@mainlyer

Description

@mainlyer

Hi. I found a weird behavior of ineritance and I don't know if this is the expected behavior or not. The issue is relative to the instanceof operator. When the declaracion/creation is inside a script, all works as I expected:

class A { }
class B extends A { }

const a = new A();
const b = new B();

console.log('typeof a == typeof b:', typeof a == typeof b); // true
console.log('typeof a === typeof b:', typeof a === typeof b); // true
console.log('a instanceof A:', a instanceof A); // true
console.log('a instanceof B:', a instanceof B); // false
console.log('b instanceof B:', b instanceof B); // true
console.log('b instanceof A:', b instanceof A); // true
console.log('B.prototype instanceof A:', B.prototype instanceof A); // true

But when coming from CLR, the behavior is different. I derived the base class from JSObject, because if not, the result is still worst.

class A : JSObject
{
    public A()
    {
        this.ValueType = JSValueType.Object;
        this.Value = this;
    }
}

class B : A
{
}

And the result is:

var context = new NiL.JS.Core.Context();

context.DefineConstructor(typeof(A));
context.DefineConstructor(typeof(B));
context.DefineVariable("a").Assign(new A());
context.DefineVariable("b").Assign(new B());

var t1 = context.Eval("typeof a == typeof b").Value; // true
var t2 = context.Eval("typeof a === typeof b").Value; // true
var t3 = context.Eval("a instanceof A").Value; // true
var t4 = context.Eval("a instanceof B").Value; // false
var t5 = context.Eval("b instanceof B").Value; // true
var t6 = context.Eval("b instanceof A").Value; // false <-------------- why ?
var t7 = context.Eval("B.prototype instanceof A").Value; // false <---- why ?

Is there a way to change this behavior throw JSObject? Am I wrong? With DefineConstant, obviously, nothing change.

Thank you in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions