JavaScrpt原型域和原型链

使用原型java

1、原型属于一类普通对象 便是Object() 自动建立,ide

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

}prototype

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

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

}ip

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

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

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

//实例化一个父实例

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 );//trueconsole.log( f.prototype instancef Object) //trueconsole.log(Fnction instaceof Object Ojbect) //true

相关文章
相关标签/搜索