Skip to content

《编写可维护的JavaScript》读书笔记 #1

@donson

Description

@donson

Page88 检测

检测原始值

//检测字符串
if(typeof name === "string"){
}

//检测数字
if(typeof coutn === "number"){
}

//检测布尔值
if(typeof found === "boolean" && found){
}

//检测 undefined
if(typeof MyApp === "undefined"){
}

//检测 null
var element = document.getElementById('my_div');
if(element !== null){
}

检测在引用值

//检测日期
if(value instanceof Date){
}

//检测正则表达式
if(value instanceof RegExp){
}

//检测 Error
�if(value  instanceof Error){
} 

检测函数

var myFun = function(){};
console.log(typeof myFun === "function");

/*
  用typeof来检测函数有一个限制,在IE8和更早版本的IE浏览器中,
  检测DOM节点中的函数都会返回“object”,而不是"function"
*/
//检测 DOM 方法
if("querySelectorAll" in document){
}

检测数组

function isArray(value){
    return Object.prototype.toString.call(value) === "[object Array]";
}

检测属性

var obj = {
    count : 0
};

if("count" in obj){
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions