为什么从数组继承在TypeScript中不起作用?
我试图从为什么从数组继承在TypeScript中不起作用?,typescript,Typescript,我试图从数组继承,但实例不起作用 class MyList<T> extends Array<T> {} const l = new MyList<string>() console.log(l instanceof MyList) // false <- WRONG console.log(l instanceof Array) // true 类MyList扩展数组{} 常量l=新的MyList() console.log(MyList的l
数组
继承,但实例
不起作用
class MyList<T> extends Array<T> {}
const l = new MyList<string>()
console.log(l instanceof MyList) // false <- WRONG
console.log(l instanceof Array) // true
类MyList扩展数组{}
常量l=新的MyList()
console.log(MyList的l instanceof)//false多亏了@jcalz,代码应该如下所示
类MyList扩展了数组{
构造函数(){
超级()
Object.setPrototypeOf(this,MyList.prototype)
}
}
常量l=新的MyList()
console.log(MyList的l instanceof)//false多亏了@jcalz,代码应该如下所示
类MyList扩展了数组{
构造函数(){
超级()
Object.setPrototypeOf(this,MyList.prototype)
}
}
常量l=新的MyList()
console.log(MyList的l instanceof)//的可能重复项的可能重复项为false
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