js类型及其判断

6个基本类型:boolean, string, number, undefined, null, symbol

判断基本类型:typeof

1 typeof 'a'; //"string"
2 typeof 1; //"number"
3 typeof true; //"boolean"
4 typeof undefined //"undefined"
5 typeof Symbol('1') //"symbol"
6 typeof null; //"object"

判断对象类型: instanceof (因为对象的 typeof 都是 'object')

1 function person(name) {
2     this.name = name
3 }
4 var mm = new person('妹妹');
5 mm instanceof person; // true

instanceof原理: 

mm.__proto__.constructor === person   或者  mm.__proto__..(原型链上的__proto__........).__proto__.constructor === person

判断数组的几种方式:

[] instanceof Array;

Array.isArray([]);

Array.prototype.isPrototypeOf([]);

Object.prototype.toString.call([]) === '[object Array]';

猜你喜欢

转载自www.cnblogs.com/vicky24k/p/11785458.html