typeof

  1. 基本类型(number、string、undefined、boolean)
1
2
3
4
5
6
typeof 1 //
typeof '1' //
typeof true //
typeof x // undefined
if(typeof x == 'undefined') // true
if(x) //直接报错
  1. function(函数)
1
2
3
typeof function(){}
typeof class C {}
typeof Math.sin
  1. object
1
2
3
typeof null // 当时设计的原因
typeof [] //
typeof {} //

instanceof

  1. 判断某个对象的是另外一个对象实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

// A 的 __proto__ 指向 B 的 prototype 时,就认为 A 就是 B 的实例
[] instanceof Array; //true
({}) instanceof Object;//true
new Date() instanceof Date;//true

//无法判断继承
function Parent(){};
function Child(){};
function Other(){};
Child.prototype = new Parent();
let child = new Child();
child instanceof Child; // true
child instanceof Parent; // true
child instanceof Object; // true
child instanceof Other; //false

//Child.prototype.__proto__ = Parent.prototype
//Parent.prototype.__proto__ = Object.prototype
//Object.prototype.__proto__ = null

Object.prototype.toString.call(obj).slice(8, -1)