檢查一個值是否是基本布爾類型,并返回 true 或 false。
基本布爾類型即 true 和 false。
function boo(bool) {
// What is the new fad diet for ghost developers? The Boolean.
return (typeof bool )=== 'boolean';
//return bool === Boolean(bool);
}
boo(null);
題目本身很簡單,下面是擴展,檢查值是否指定類型Object.prototype.toString();返回對象所屬類的名稱
function isType(value,type){
var res=Object.prototype.toString.call(value);
return res === '[Object'+type+']';
}
還可以用正則優化,忽略第二個參數的大小寫