typeof 和instanceof

typeof

typeof操作符返回一个字符串,表示未经计算的操作数的类型。

语法:
typeof operand 或者 typeof (operand)
operand 是一个表达式,表示对象或原始值,其类型将被返回。括号是可选的。

有关的typeof可能的返回值。
1.Undefined 返回 "undefined"。
2.Null 返回 "object"。
3.Boolean 返回 "boolean"。
4.Number 返回 "number"。
5.String 返回 "string"。
6.Symbol 返回 "symbol"。
7.宿主对象 返回 Implementation-dependent。
8.函数对象 返回 "function"。

使用 new 操作符:
var fn1 = new String()
fn1//String {"", length: 0}length: 0__proto__: String[[PrimitiveValue]]: ""
fn1 instanceof String
//true
typeof fn1
//"object"
fn2 = new Function()
typeof fn2
//"function"

instanceof

instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。

语法:
object instanceof constructor
object,要检测的对象.constructor,某个构造函数。
instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上。

instanceof 返回的值是 Boolean。

例如:
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var mycar = new Car("Honda", "Accord", 1998);
var a = mycar instanceof Car; // 返回 true
var b = mycar instanceof Object; // 返回 true