1

I tried to inherit from Array, but the instanceof not working.

class MyList<T> extends Array<T> {}

const l = new MyList<string>()
console.log(l instanceof MyList)  // false <- WRONG
console.log(l instanceof Array)   // true
2

1 Answer 1

0

Thanks to @jcalz the code should be as below, more details

class MyList<T> extends Array<T> {
    constructor() {
        super()
        Object.setPrototypeOf(this, MyList.prototype)
    }

}
const l = new MyList<string>()
console.log(l instanceof MyList)  // false <- WRONG
console.log(l instanceof Array)   // true
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.