We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Js中如何准确判断一个变量是什么,面试中这是考一个人基本功扎不扎实必定会问的一个问题
1.typeof
我们能够使用typeof判断变量的身份,判断字符串得到string,数字和NaN得到number,函数会得到function等,但是判断数组,对象和null时都会得到object,详细请看js数据类型,这就是typeof的局限性,并不能准确的判断该变量的"真实身份"。那如何判断一个变量是数组还是对象?
2.instanceof
使用instanceof可以用来判断一个变量是数组还是对象,原理如下:
数组也是对象的一种,使用instanceof都会返回true
var arr = new Array();
var arr = ['aa','bb','cc']; var obj = { a: 'aa', b: 'bb', c: 'cc' }; console.log(arr instanceof Array); //true console.log(arr instanceof Object); //true console.log(obj instanceof Array); //false console.log(obj instanceof Object); //true
明确说一下instanceof是如何判断的:
instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性,意思就是该变量通过原型链上能否找到构造函数的prototype 属性,还不清楚原型链的请看原型链
所以就能明白为什么instanceof判断一个变量可以分清楚它到底是数组还是对象:
Array.prototype === arr.__proto__ Object.prototype === arr.__proto__.__proto__
因为arr的原型链上存在Array.prototype和Object.prototype 只有Array类型的变量才会满足arr instanceof Array和arr instanceof Object都返回true, 也只有Object类型变量才满足obj instanceof Array返回false,obj instanceof Object返回true
3.constructor
var arr = ['aa','bb','cc']; var obj = { 'a': 'aa', 'b': 'bb', 'c': 'cc' }; console.log(arr.constructor === Array); //true console.log(arr.constructor === Object); //false console.log(obj.constructor === Object); //true
4.Object.prototype.toString.call()
Object.prototype.toString.call()方法可以精准判断变量类型,它返回[object constructorName]的字符串格式,这里的constructorName就是call参数的函数名
var a = NaN; var b= '222'; var c = null; var d = false; var e = undefined; var f = Symbol(); var arr = ['aa','bb','cc']; var obj = { 'a': 'aa', 'b': 'bb', 'c': 'cc' }; var res = Object.prototype.toString.call(arr); console.log(res); //[object Array] var res2 = Object.prototype.toString.call(obj); console.log(res2); //[object Object] var res3 = Object.prototype.toString.call(a); console.log(res3); //[object Number] var res4 = Object.prototype.toString.call(b); console.log(res4); //[object String] var res4 = Object.prototype.toString.call(c); console.log(res4); //[object Null] var res5 = Object.prototype.toString.call(d); console.log(res5); //[object Boolean] var res6 = Object.prototype.toString.call(e); console.log(res6); //[object Undefined] var res7 = Object.prototype.toString.call(f); console.log(res7); //[object Symbol]
判断简单数据类型可以用typeof,判断数组,对象使用instanceof,constructor和 Object.prototype.toString.call(),最好使用Object.prototype.toString.call(),更加精准
The text was updated successfully, but these errors were encountered:
No branches or pull requests
一,判断方法
1.typeof
我们能够使用typeof判断变量的身份,判断字符串得到string,数字和NaN得到number,函数会得到function等,但是判断数组,对象和null时都会得到object,详细请看js数据类型,这就是typeof的局限性,并不能准确的判断该变量的"真实身份"。那如何判断一个变量是数组还是对象?
2.instanceof
使用instanceof可以用来判断一个变量是数组还是对象,原理如下:
数组也是对象的一种,使用instanceof都会返回true
明确说一下instanceof是如何判断的:
instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性,意思就是该变量通过原型链上能否找到构造函数的prototype 属性,还不清楚原型链的请看原型链
所以就能明白为什么instanceof判断一个变量可以分清楚它到底是数组还是对象:
3.constructor
4.Object.prototype.toString.call()
Object.prototype.toString.call()方法可以精准判断变量类型,它返回[object constructorName]的字符串格式,这里的constructorName就是call参数的函数名
二,总结
判断简单数据类型可以用typeof,判断数组,对象使用instanceof,constructor和 Object.prototype.toString.call(),最好使用Object.prototype.toString.call(),更加精准
The text was updated successfully, but these errors were encountered: