typeof用以获取一个变量或者表达式的类型
typeof一般只能返回如下几个结果:
number,boolean,string,function(函数),object(NULL,数组,对象),undefined。
alert(typeof (123));//typeof(123)返回"number"
alert(typeof ("123"));//typeof("123")返回"string"
使用typeof来获取一个变量是否存在
if(typeof a!="undefined"){},而不要去使用if(a)因为如果a不存在(未声明)则会出错,
因为typeof遇到null,数组,对象时都会返回object类型,所以当我们要判断一个对象是否是数组时
或者判断某个变量是否是某个对象的实例则要选择使用另一个关键语法instanceof
instanceof用于判断一个变量是否某个对象的实例
如var a=new Array();alert(a instanceof Array);会返回true,
同时alert(a instanceof Object)也会返回true;这是因为Array是object的子类。
再如:
function test(){};
var a=new test();
alert(a instanceof test)会返回true。
链接:http://blog.csdn.net/u014421556/article/details/52083197
instanceof 运算符
instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上。
实例一:普遍用法
A instanceof B :检测B.prototype是否存在于参数A的原型链上.
function Ben() {
}
var ben = new Ben();
console.log(ben instanceof Ben);//true
实例二:继承中判断实例是否属于它的父类
function Ben_parent() {}
function Ben_son() {}
Ben_son.prototype = new Ben_parent();//原型继承
var ben_son = new Ben_son();
console.log(ben_son instanceof Ben_son);//true
console.log(ben_son instanceof Ben_parent);//true
实例三:复杂用法
function Ben() {}
console.log(Object instanceof Object); //true
console.log(Function instanceof Function); //true
console.log(Function instanceof Object); //true
console.log(Ben instanceof Function); //true
console.log(String instanceof String); //false
console.log(Boolean instanceof Boolean); //false
console.log(Ben instanceof Ben); //false
看到上述的结果,是否有点懵了,究其原因,还需探其原理,下面我们来看看规范中如何定义的。
ECMASCRIPT 5.1 Standard文档中的定义: 11.8.6 The instanceof operator The production RelationalExpression : RelationalExpression instanceof ShiftExpression is evaluated as follows: 1.Let lref be the result of evaluating RelationalExpression. 2.Let lval be GetValue(lref). 3.Let rref be the result of evaluating ShiftExpression. 4.Let rval be GetValue(rref). 5.If Type(rval) is not Object, throw a TypeError exception. 6.If rval does not have a [[HasInstance]] internal method, throw a TypeError exception. 7.Return the result of calling the [[HasInstance]] internal method of rval with argument lval. 15.3.5.3 [[HasInstance]] (V) Assume F is a Function object. When the [[HasInstance]] internal method of F is called with value V, the following steps are taken: //V不是对象,直接返回false 1.If V is not an object, return false. //用 [[Get]] 方法取 F方法的prototype属性,结果赋值给O 2.Let O be the result of calling the [[Get]] internal method of F with property name "prototype". //如果O不是一个对象,报告异常 3.If Type(O) is not Object, throw a TypeError exception. //重复循环 4.Repeat //V = V.[[Prototype]] a.Let V be the value of the [[Prototype]] internal property of V. b.If V is null, return false. c.If O and V refer to the same object, return true. NOTE Function objects created using Function.prototype.bind have a different implementation of [[HasInstance]] defined in 15.3.4.5.3.
对应上述规范做个函数模拟A instanceof B:
function _instanceof(A, B) {
var O = B.prototype;// 取B的显示原型
A = A.__proto__;// 取A的隐式原型
while (true) {
//Object.prototype.__proto__ === null
if (A === null)
return false;
if (O === A)// 这里重点:当 O 严格等于 A 时,返回 true
return true;
A = A.__proto__;
}
}
使用此函数模拟解析过程:
Object instanceof Object解析,执行_instanceof (Object, Object) O = Object.prototype; A = Object.__proto__ = Function.prototype A = Function.prototype.__proto__ = Object.prototype return true; Function instanceof Function解析,执行_instanceof (Function, Function) O = Function.prototype; A = Function.__proto__ = Function.prototype; return true; Function instanceof Object解析,执行_instanceof (Function, Object) O = Object.prototype; A = Function.__proto__ = Function.prototype; A = Function.prototype.__proto__ = Object.prototype; return true; String instanceof String解析,执行_instanceof (String, String) O = String.prototype; A = String.__proto__ = Function.prototype; A = Function.prototype.__proto__ = Object.prototype; A = Object.prototype.__proto__ = null; return false; Ben instanceof Ben解析,执行_instanceof (Ben, Ben) O = Ben.prototype; A = Ben.__proto__ = Ben.prototype; A = Ben.prototype.__proto__ = Object.prototype; A = Object.prototype.__proto__ = null; return false;
参考链接:
http://www.zuojj.com/tdocs/es5.1/#sec-11.8.6
http://blog.csdn.net/cuew1987/article/details/15498121
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/instanceof
浙公网安备 33010602011771号