//p 是Person的一个实例。是依据什么来判断呢?
function Person(){
}
Person.prototype = {};
var p = new Person();
alert(p instanceof Person);//true.我把Person的prototype重新指向一个简单Object对象实例。是不是p里面有什么属性可以用于判断它是Person的一个实例?
还是说,这是javascript的内建东西,不开放给程序员?对于一个简单的基础来说,也同样的情况:function Person(){
}
Person.prototype = {};
var p = new Person();
alert(p instanceof Person);//true.function Boy(){
}
Boy.prototype = new Person();
Boy.prototype.constructor = Boy;
var b = new Boy();
alert(b instanceof Boy);//true why??
alert(b instanceof Person);//true why??
function Person(){
}
Person.prototype = {};
var p = new Person();
alert(p instanceof Person);//true.我把Person的prototype重新指向一个简单Object对象实例。是不是p里面有什么属性可以用于判断它是Person的一个实例?
还是说,这是javascript的内建东西,不开放给程序员?对于一个简单的基础来说,也同样的情况:function Person(){
}
Person.prototype = {};
var p = new Person();
alert(p instanceof Person);//true.function Boy(){
}
Boy.prototype = new Person();
Boy.prototype.constructor = Boy;
var b = new Boy();
alert(b instanceof Boy);//true why??
alert(b instanceof Person);//true why??
1. 求表达式RelationalExpression的值;
2. 对步骤1的返回值,调用getValue(V)方法;
3. 求表达式ShiftExpression的值;
4. 对步骤3的返回值,调用getValue(V)方法;
5. 如果步骤4返回的不是一个object类型,抛出TypeError异常;
6. 如果步骤4的返回结果没有[[HasInstance]]方法,抛出TypeError异常;
7. 对步骤4的结果调用[[HasInstance]]方法,以步骤2的返回值为参数;
8. 返回7的结果。nstanceof可以检测某个对象是不是另一个对象的实例:
function Pig() {}
var pig = new Pig();
alert(pig instanceof Pig); // => truefunction FlyPig() {}
FlyPig.prototype = new Pig();
var flyPig = new FlyPig();
alert(flyPig instanceof Pig); // => true创建实例后,只要原型重新被赋值,对象的类型就会丢失:
function Sunwukong() {}
Sunwukong.prototype = {
"72变": function() {
},
"筋斗云": function() {
}
};
var sunxingzhe = new Sunwukong();
alert(sunxingzhe instanceof Sunwukong); //=> trueSunwukong.prototype = {
"72变": function() {
},
"筋斗云": function() {
}
};
alert(sunxingzhe instanceof Sunwukong); //=> falseSunwukong.prototype = {
"36变": function() {
},
"高老庄女婿": function() {
}
};
var zhubajie = new Sunwukong();
alert(sunxingzhe instanceof Sunwukong); //=> false
alert(zhubajie instanceof Sunwukong); //=> true
如果V不是引用类型,返回V;
调用getBase(V)方法,返回V的基本对象类型;
如果步骤二返回null,抛出ReferenceError异常;
调用步骤二返回值的[[Get]]方法,通过GetPropertyName(V)求原型名称;
返回第四步的值。GetBase(V). Returns the base object component of the reference V.
GetPropertyName(V). Returns the property name component of the reference V.
var a = new Person();
alert(a instanceof Person);//truePerson.prototype = {
constructor:Person
}alert(a instanceof Person);//falsePS:ECMA不好看懂,大概只知道js内置了机制去判断关系了。