標籤:bsp 一個 child script 賦值 erro 原型式繼承 函數實現 寄生式繼承
1.js建立對象的幾種方式
原廠模式
為什麼會產生原廠模式,原因是使用同一個介面建立很多個物件,會產生大量的重複代碼,為瞭解決這個問題,產生了原廠模式。
function createPerson(name,age,job){
var o=new Object();
o.name=name;
o.age=age;
o.job=job;
o.sayName=function(){
console.log(this.name);
}
return o;
}
var person1=createPerson("kobe","34","player");
var person2=createPerosn("patty","32","singer");
建構函式模式
與原廠模式不同的是,沒有顯示的建立對象,直接將屬性和方法賦值this對象,沒有return語句。
function Person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
this.sayName=function(){
console.log(this.name);
};
}
var person1=new Person();
var person2=new Person();
console.log(person1.sayName==person2.sayName)//false 說明不同執行個體的同名函數是不相等的
如果我們想要的結果是兩者相等,可以這樣實現
function Person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
this.sayName=sayName;
}
function sayName(){
console.log(this.name);
}
var person1=new Person();
var person2=new Person();
console.log(person1.sayName==person2.sayName);//true
建立Person的新執行個體,需要用到new操作符,這種方式下調用建構函式會經曆四個階段,分別是:
建立一個新對象
將建構函式的範圍賦值給這個新的對象
執行建構函式中的代碼
返回新對象
person1和person2這兩個對象都有一個constructor屬性,該屬性指向Person
console.log(person1.constructor==Person);//true
console.log(person2.constructor==Person);//true
原型模式
特點:新對象的這些屬性和方法是所有執行個體共用的
function Person(){
}
Person.prototype.name="kobe";
Person.prototype.age=38;
Person.prototype.sayName=function(){
console.log(this.name);
}
var person1=new Person();
var person2=new Person();
console.log(person1.sayName==person2.sayName);//true
有時候我們想知道該屬性到底是存在對象中還是存在原型中,可以使用以下方法
我們使用in操作符和hasOwnProperty結合判斷
"name" in object無論該屬性到底存在原型中還是對象中,都會返回true
而hasOwnProperty只有存在執行個體中才返回true
so:只有in操作符返回true,而hasOwnProperty返回false,能確定屬性是原型中的屬性。
function hasPrototypeProperty(object,name){
return !object.hasOwnProperty(name)&&(name in object);
}
原型對象存在問題,牽一髮而動全身
function Person(){
}
Perosn.prototype=function(){
constructor;Person,
name:"kobe",
age:"29",
job:"player",
friends:["shely","count"],
sayName:function(){
console.log(this.name);
}
};
var person1=new Person();
var person2=new Person();
person1.friends.push("ann");
console.log(person1.friends===person2.friends);//true
解決的方法:是使用建構函式模式和原型模式
function Person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
this.friends=["she","ya"];
}
Person.prototype={
constructor:Person,
sayName:function(){
console.log(this.name);
}
};
var person1=new Person();
var person2=new Person();
person1.friends.push("VAN");
console.log(person1.friends===person2.friends);//false
動態原型模式
function Person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
if(typeof this.sayName!="function"){
Person.prototype.sayName=function(){
console.log(this.name);
}
};
}
寄生建構函式模式
function Person(name,age,job){
var o=new Object();
o.name=name;
o.age=age;
o.job=job;
o.sayName=function(){
console.log(this.name);
};
return o;
}
var friend=new Person();//此模式與原廠模式十分類似
2.js實現繼承的幾種方式
原型鏈繼承:原型對象屬性共用
function Parent2(){
this.name="kobe";
this.play=[1,2,3];
}
function Child2(){
this.type="children";
}
Child2.prototype=new Parent2();
var say1=new Child2();
var say2=new Child2();
say1.play.push("van");
console.log(say1.play==say2.play);//true
借用建構函式實現繼承:無法實現繼承原型對象
function Parent1(){
this.name="kobe";
}
Parent1.prototype.age=90;
function Child(){
Parent1.call(this);
this.type="service";
}
var say=new Child();
console.log();//error
組合式繼承
function Parent4(name){
this.name="kobe";
this.play=[1,2,3];
}
Parent4.prototype.sayName=function(){
}
function Child4(name,age){
Parent3.call(this,name);
this.age=age;
}
Child4.prototype=new Parent4();
Child4.prototype.constructor=Child4;
Child4.prototype.sayAge=function(){
console.log(this.age);
};
var ins1=new Child4();
var ins2=new Child4();
ins1.push.push(4);
console.log(ins1.play==ins2.play);//false
原型式繼承
function object(){
function F(){}
F.prototype=o;
return new F();
}
var person={
name:"kobe",
friends;["yang","du","geng"]
};
var onePerson=object(person);
var twoPerson=object(person);
寄生式繼承
function object(o){
function F(){}
F.prototype=o;
return new F();
}
function create(o){
var clone=object(o);
clone.sayHi=function(){
console.log("hi");
};
return clone;
}
var person={
name:"kobe",
friends:["james","waston","sun"]
};
var anotherPerson=creat(person);
anotherPerson.sayHi();//hi
寄生式組合繼承
function inheritPrototype(Child5,Parent5){
var prototype=Object(Parent5.prototype);
prototype.constructor=Child5;
Child5.prototype=prototype;
}
function Parent5(name){
this.name=name;
this.colors=["red","blue","green"];
}
Parent5.prototype.sayName=function(){
console.log(this.name);
};
function Child5(name,age){
Parent5.call(this.name);
this.age=age;
}
inheritPrototype(Child5,Parent5);
Child5.prototype.sayAge=function(){
console.log(this.age);
};
javascript(js)建立對象的模式與繼承的幾種方式