使用原型

一、原型属于一类普通对象 即是Object() 自动创建,

1、通过原型添加属性 function a(x){ this.x = x;

}

a.prototype.x = 2 //添加属性 var a1=new a(4) a.prototype.x= a1.x //将本地属性传递给原型属性 2、使用原型添加方法 和 使用原型来继承

function a(x,y,z){ this.x=x; this.y=y; this.z=z;

}

a.proptype.add=function(a,b){ //添加方法

return a+b; } 使用原型实现继承 function Parent(x){ this.x=x console.log(x) }

function Child(y){ this.y=y; console.log(y) }

//实例化一个父实例

var parent = new Parent("父");

Child.proptype=parent;

//实现一个子实例

var child=new Child("子")

child.x child.y

原型域和原型域链 在javaScript 在实例读取对象属性时候,总是先检查自己本地属性,假如存在,则返回本地属性,如果不存在则往通过prototype原型域查找,返回原型域中的属性

Function.prototype.a=function(){ alert("Function") } object.prototype.a =function(){ alert("Oject")

} function f(){ this.a = a }

f.prootype ={ n:function(){

alert("w") } }

console.log( f instancef Function );//true console.log( f.prototype instancef Object) //true console.log(Fnction instaceof Object Ojbect) //true