标签:cti str exce when pre stack code flow com
typeof和instanceof可以用来判断一个数据的类型,什么时候选择使用typeof?什么时候选择使用instanceof?typeof运算符typeof运算符返回值有以下几种
typeof 123 // "number"
typeof '123' // "string"
typeof false // "boolean"
function f() {}
typeof f
// "function"
typeof undefined
// "undefined"
var x
typeof x
// "undefined
typeof window // "object"
typeof {} // "object"
typeof [] // "object"
typeof null // "object
instanceof运算符instanceof是判断变量是否为某个对象的实例,返回值为true或false。
var o = {};
var a = [];
o instanceof Array // false
a instanceof Array // true
a instanceof Object // true
typeof 对数组 [] 和对象 {} 的返回值都是Object,无法区分数组和对象,但是instanceof可以区分。
typeof和instanceofalert(typeof undefinedVariable); // alerts the string "undefined"
alert(undefinedVariable instanceof Object); // throws an exception
instanceof运算符var myNullVar = null;
alert(typeof myNullVar ); // alerts the string "object"
alert(myNullVar instanceof Object); // alerts "false"
https://www.jianshu.com/p/1216b1f429fb
关于JavaScript中的typeof与instanceof
标签:cti str exce when pre stack code flow com
原文地址:https://www.cnblogs.com/yuanchao-blog/p/JavaScript.html