[英]How to bind an array of json objects to an array of javascript class in ES6
[英]How to Construct an array by es6 class
我期望一个类在我new
时返回一个数组
class MyArray {
constructor(){
}
}
const myArray = new MyArray()
Array.isArray(myArray) // Should be true
我以前是这样写的:
class MyArray {
constructor(){
const arry = new Array()
return arry
}
}
但是,当我在写打字稿,返回值arry
不是MYARRAY的类型,所以它提示错误。
如何解决这个问题?
只需扩展Array
并在构造函数中 return true
class MyArray extends Array{
constructor(){
super()
}
}
演示版
class MyArray extends Array{ constructor(){ super() } } myArray2 = new MyArray() console.log(Array.isArray(myArray2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.