javascript中的typeof和型別判斷
ECMAScript 有 5 種原始型別(primitive type),即 Undefined、Null、Boolean、Number 和 String。我們都知道可以使用typeof運算子求得一個變數的型別,但是對引用型別變數卻只會返回object
var a = "abc";
console.log(typeof a); // "string"var b = 123;
console.log(typeof b); // "number"var c = true;
console.log(typeof c); // "boolean"var d = null;
console.log(typeof d); // "object"var f = undefined;
console.log(typeof f); // "undefined"var g;
console.log(typeof g); // "undefined"
console.log(typeof x); // "undefined"
var a = function() { };
console.log(typeof a); // "function"var b = [1,2,3];
console.log(typeof b); // "object"var c = { };
console.log(typeof c); // "object"
toString.call(a); // "[object Array]"
a instanceof Array; //true
a.constructor == Array; //true
instanceof
var a = [1,2,3,4,5];
console.log(toString.call(a)); // "[object Array]"
console.log(a instanceof Array); //true
console.log(a.constructor == Array); //true
JSON.stringify(obj); // "{}"
if(obj.id){ //如果屬性id存在....}
function isEmptyObject(e) { var t; for (t in e) return !1; return !0 } //trueisEmptyObject(obj); //falseisEmptyObject({ "a":1, "b":2});