首页 > 代码库 > js原型链接(二)

js原型链接(二)

原型链的内部执行方式

<script>function Myclass(){    this.x=" x in Myclass";}var obj=new Myclass();p(obj.x);p(obj.z); //undefinedMyclass.prototype.z="z in Myclass";p(obj.z);//首先查找自身属性,如果没有找到 将沿着原型链接 查找构造函数(Myclass)的prototype对象里找</script>

属性的重写与删除与原型链无关

<script>function Myclass(){    this.x=" x in Myclass";}Myclass.prototype.y="y in Myclass";var obj=new Myclass();p(obj.y);//y in Myclassobj.y="override y";p(obj.y);//override ydelete obj.y //truep(obj.y);//y in Myclassvar obj2=new Myclass();p(obj2.y);//y in Myclassobj.z=zzz;  p(obj.z);//zzzp(obj2.z);//undefinedp(obj.prototype);//undefined</script>

获取原型对象的三种方法

<script>function Myclass(){}var proto=Myclass.prototype;var obj=new Myclass();//通过第五版里加强var proto=Object.getPrototypeOf(obj);//通过对象实例获得var proto=obj.__proto__;//通过对象实例以及其构造函数var proto=obj.constructor.prototype;p(obj.constructor==Myclass); //true</script>

通过constructor判定数据类型

 

<script>var d=new Date();p(d.constructor);//function Date() { [native code] }var arr=[1,2,3];p(arr.constructor);//function Array() { [native code] }var obj={};p(obj.constructor);//function Object() { [native code] }</script>

 

constructor属性并不是对象的直接属性,而是通过原型链接 查找到的
每个对象在创建时 构造器会执行这样一句代码
this.prototype=
{
constructor:this,
__proto__:Object.prototype
}
通过改变prototype实现继承

<script>function Derived(){}  //创建时 就有了Derived.prototype={constructor:Derived}p(Derived.prototype.__proto__==Object.prototype);//truep(Derived.__proto__);//function Empty() {}function Base(){} //原理同上Derived.prototype=new Base();//此时Derived.prototype的原型链接__proto__改变了指向p(Derived.prototype.__proto__==Base.prototype); //truevar obj=new Derived();//此时p(obj.__proto__==Derived.prototype);//true//现在obj里找 ,没有,到Derived.prototype引用的对象Base里找,没有,就到Base.prototype里找了p(obj.constructor); //function Base(){}</script>

数据类型判定(instanceof与isPrototypeOf)

<script>var d=new Date();p(Date.__proto__);////function Empty() {}p(d instanceof Date);//truep(d instanceof Object);//truep(Date instanceof Object);//truep(Object instanceof Date);//falsefunction Derived(){}function Base(){}Derived.prototype=new Base();var obj=new Derived();p(obj instanceof Derived);//truep(obj instanceof Base); //truep(Derived instanceof Base);//false   Derived不是由 Base构造函数生成的p(Derived.constructor);//function Function() { [native code] }p(Derived instanceof Object);//truep(Derived.prototype.isPrototypeOf(obj)); //trueBase.prototype.isPrototypeOf(obj);//trueObject.prototype.isPrototypeOf(obj)//true</script>

属性的枚举
in 可以可判断本身属性和通过继承来的属性 是否存在于某个对象
hasOwnProperty只列出本身可以枚举的属性
有些属性被枚举出来是因为enumerable属性为false
getOwnPropertyNames可以无视枚举属性,列举出所有属性

 

<script>var obj={x:1,y:2};p(Object.keys(obj));//x,yobj.z=3;//obj.prototype.p=‘pp‘;//实例 prototype属性p(obj.prototype);//undefinedp(obj.__proto__==Object.prototype); //truep(Object.keys(obj));//x,y,xvar arr=[1,2,3];p(Object.keys(arr)); //0,1,2p(Object.getOwnPropertyNames(arr)); //0,1,2,lengthp(Object.keys(Object.prototype));//p(Object.getOwnPropertyNames(Object.prototype));/*constructor,toString,toLocaleString,valueOf,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,__defineGetter__,__lookupGetter__,__defineSetter__,__lookupSetter__,__proto__*///对于enumerable可根据propertyIsEnumerable来判断function Myclass(){    this.x=1;this.y=2;}Myclass.prototype.z=3;var obj=new Myclass();p(Object.getOwnPropertyNames(obj)); //x,y 不列举通过继承获取的属性p(Object.keys(obj));// x,yfor(var key in obj){    p(key);}//x,y,x</script>

 

js原型链接(二)