3 回答

TA貢獻1788條經驗 獲得超4個贊
嘗試使用instanceof運算符
const arr = [];
console.log(arr instanceof Array); // true
const obj = {};
console.log(obj instanceof Array); // false

TA貢獻1784條經驗 獲得超7個贊
因為 anarray
在技術上是一種類型object
- 只是具有某些能力和行為,例如附加方法Array.prototype.push()
和Array.prototype.unshift()
。數組是常規對象,其中整數鍵屬性和長度屬性之間存在特定關系。
要確定您是否有專門的數組,您可以使用 Array.isArray()
.

TA貢獻2003條經驗 獲得超2個贊
在 JavaScript 中,幾乎所有東西都是對象。
它使用原型鏈來實現繼承。
您可以只使用 console.log( [] ) 并查看原型部分以查看它是從對象繼承的。
這是制作自己的數組的簡單方法。
function MyArray(){
Object.defineProperty(this, 'length', {
value: 0,
enumerable: false,
writable: true,
})
}
MyArray.prototype.push = function(elem){
this[this.length] = elem
this.length++
return this.length
}
MyArray.prototype.isMyArray = function(instance){
return instance instanceof MyArray
}
var arr = new MyArray()
arr.push(1)
arr.push(2)
arr.push(3)
console.log('instance', MyArray.prototype.isMyArray( arr ))
// instance true
console.log('object', typeof arr)
// object object
添加回答
舉報