var name = "Jack"; var age = 32; var single = false; var app; //undefined console.log(typeof name); //string console.log(typeof age); //number console.log(typeof single); //boolean console.log(typeof app); //undefined console.log(typeof null); //object
if(typeof name === "string") { name += "Zhang"; }
if(typeof age === "number") { age++; }
if(typeof single === "boolean" && single) { … }
if(typeof app === "undefined") { app = {}; }
if(el === null) { … }
console.log(typeof new Date()); //object console.log(typeof new RegExp()); //object console.log(typeof new Error()); //object console.log(typeof new Person()); //用typeof检测出自定义对象也是object
var date = new Date();
var reg = new RegExp();
var err = new Error();
var me = new Person();
if(date instanceof Date) { //检测日期
year = date.getFullYear();
}
if(reg instanceof RegExp) { //检测正则表达式
reg.test(...);
}
if(err instanceof Error) { //检测异常
throw err;
}
if(me instanceof Person) { //检测自定义对象
...
}
var func = function(){};
if(typeof func === 'function') { … }
console.log(typeof document.getElementById); //object,不是function
console.log(typeof document.getElementsByTagName); //object,不是function
console.log(typeof document.createElement); //object,不是function
//IE8以前的IE浏览器,要改用in来检测是否支持DOM函数
if("getElementById" in document) { … }
if("getElementsByTagName" in document) { … }
if("createElement" in document) { … }
function isArray(arr) {
return Object.prototype.toString.call(arr) === "[Object Array]";
}
function isArray(arr) {
return typeof arr.sort === "function";
}
var Person = {
name: "Jack",
age: 33
};
if("name" in Person) { … } //true
if(Person.hasOwnProperty("name")) { … } //true
var Person = function (name, age) {
this.name = name;
this.age = age;
};
Person.prototype.location = "Shanghai";
var me = new Person("Jack", 33)
if("name" in me) { … } //true
if(me.hasOwnProperty("name")) { … } //true
if("location" in me) { … } //true
if(me.hasOwnProperty("location")) { … }//false
if (object[propName]) //Not Good,你怎么知道属性值不是0或1? if (object[propName] === null) //Not Good,你怎么知道属性值不是null? if (object[propName] === undefined) //Not Good,你怎么知道属性值不是undefined?
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有