JavaScript 演練(3). 判斷是否是數組



num = 123;
str = "123";

alert(num == 123); //true
alert(str == 123); //true

alert(num === 123); //true; 值相同且類型相同
alert(str === 123); //false

alert(typeof num === "number"); //true
alert(typeof str === "string"); //true

//數組的類型也是 object
alert(typeof []); //object
alert(typeof {}); //object


//判斷是否是數組的函數
var isArray = function (obj) {
    //return obj && !(obj.propertyIsEnumerable('length')) && typeof obj === 'object' && typeof obj.length === 'number';
    return obj instanceof Array;
};

arr = [ 1, 2, 3 ];
obj = { a: 1, b: 2, c: 3 };
alert(isArray(arr)); //true
alert(isArray(obj)); //false
alert(isArray(num)); //false
alert(isArray(str)); //false


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM