JavaScrpt prototype and prototype chain domain

Prototype

A prototype belongs to a class that is a general object Object () is automatically created,

1, by adding attributes prototype
function A (X) {
in this.x = X;

}

a.prototype.x = 2 // add attributes
var new new A1 = A (. 4)
a.prototype.x = a1.x // pass prototype property local property to
2, and the addition method using the prototype with prototypes inheritance

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

}

a.proptype.add = function (a, b) {// add methods

A + B return;
}
prototype implementation inheritance
function the Parent (X) {
in this.x X =
the console.log (X)
}

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

// instantiate an instance of the parent

var parent = new Parent("父");

Child.proptype=parent;

// achieve a sub-instance

var child=new Child("子")

child.x
child.y

Chain domain domain prototype and prototype
read javaScript object properties in the example, always check its local attributes, if present, the local property is returned, and if not then the prototype to the prototype by domain lookup returns prototype domain properties

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

Guess you like

Origin blog.51cto.com/14582569/2476398