typeof 用于区分原始类型 和 对象类型。
instanceof 用户与区分不同的对象类型。
var s = “Nicholas”;
var b = true;
var i = 22;
var u;
var n = null;
var o = new Object();
alert(typeof s); //string
alert(typeof i); //number
alert(typeof b); //boolean
alert(typeof u); //undefined
alert(typeof n); //object
alert(typeof o); //object
instanceof 用户与区分不同的对象类型。
var Person = function(){}
var person = new Person();
alert(person instanceof Person); //true is the variable person an Person ?
alert(person instanceof Object); //true is the variable person an Object?
alert(colors instanceof Array); //is the variable colors an Array?
alert(pattern instanceof RegExp); //is the variable pattern a RegExp?
本文介绍了JavaScript中使用typeof关键字来区分原始类型和对象类型的用法,并通过实例展示了如何判断变量的具体类型,包括基本数据类型如字符串、数字、布尔值等,以及复杂数据类型如对象、数组、正则表达式的实例判断。
1467

被折叠的 条评论
为什么被折叠?



