陣列始終將對象的實例 - Object
是JavaScript和任何由新創建的另一個目的是繼承基礎對象從。
new String('a') instanceof Object // true - also instance of String
new Number(3) instanceof Object // true -also instance of Number etc.
new Boolean(true) instanceof Object // true
new Date instanceof Object // true
new function(){} instanceof Object // true
[] instanceof Object // true - [] is equal to new Array
check this out:
Array = 1;
[] //TypeError: Cannot read property 'slice' of undefined
:)
然而
'a' instanceof Object // false
3 instanceof Object // false
試試這個:
var str = 'aaa',
arr = [],
myClass = function(){},
myClassInstance = new myClass;
str.constructor == String // true
arr.constructor == Array // true
myClassInstance.constructor == myClass // true
順便說一句,[這已經問(http://stackoverflow.com/questions/4775722/javascript-check-if-object -is陣列)。 – voithos