[英]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.