1:原型繼承
為了讓子類繼承父類的屬性(也包括方法),首先需要定義一個(gè)構(gòu)造函數(shù)盟蚣。然后黍析,將父類的新實(shí)例賦值給構(gòu)造函數(shù)的原型。代碼如下:
eg:原型繼承
//全是改變的指針模擬繼承 new this指針改變 通過(guò)綁定prototype復(fù)制指針改變
function proto(){
this.name="caoshiyuan"http://如果this不懂看下一個(gè)類式繼承
}
function child(){
this.age=23
}
function generator(){
var name=123
}
console.log(child.constructor)
console.log(proto.constructor)
console.log(generator.constructor())
console.log(generator.constructor)
// ------------以上可以略過(guò)
child.prototype=new proto()
let childs=new child()
console.log(childs.name)//caoshiyuan
console.log(childs)//沒(méi)有name
console.log(childs.proto)//只有name指向原型 proto指向?qū)嵗瘮?shù)的原型 他的原型是一個(gè)指針指向proto函數(shù)
console.log(child.prototype)//和上面相等
2:類式繼承
主要使用call apply 方法實(shí)現(xiàn)繼承
1屎开、方法定義
call方法: 語(yǔ)法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定義:調(diào)用一個(gè)對(duì)象的一個(gè)方法阐枣,以另一個(gè)對(duì)象替換當(dāng)前對(duì)象。 說(shuō)明: call 方法可以用來(lái)代替另一個(gè)對(duì)象調(diào)用一個(gè)方法。call 方法可將一個(gè)函數(shù)的對(duì)象上下文從初始的上下文改變?yōu)橛?thisObj 指定的新對(duì)象蔼两。 如果沒(méi)有提供 thisObj 參數(shù)甩鳄,那么 Global 對(duì)象被用作 thisObj。 apply方法: 語(yǔ)法:apply([thisObj[,argArray]]) 定義:應(yīng)用某一對(duì)象的一個(gè)方法额划,用另一個(gè)對(duì)象替換當(dāng)前對(duì)象妙啃。 說(shuō)明: 如果 argArray 不是一個(gè)有效的數(shù)組或者不是 arguments 對(duì)象,那么將導(dǎo)致一個(gè) TypeError俊戳。 如果沒(méi)有提供 argArray 和 thisObj 任何一個(gè)參數(shù)揖赴,那么 Global 對(duì)象將被用作 thisObj, 并且無(wú)法被傳遞任何參數(shù)品抽。
eg: call
function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub,3,1);
這個(gè)例子中的意思就是用 add 來(lái)替換 sub储笑,add.call(sub,3,1) == add(3,1) ,所以運(yùn)行結(jié)果為:alert(4); // 注意:js 中的函數(shù)其實(shí)是對(duì)象圆恤,函數(shù)名是對(duì) Function 對(duì)象的引用突倍。
b、
function Animal(){
this.name = "Animal";
this.showName = function(){
alert(this.name);
}
}
function Cat(){
this.name = "Cat";
}
var animal = new Animal();
var cat = new Cat();
//通過(guò)call或apply方法盆昙,將原本屬于Animal對(duì)象的showName()方法交給對(duì)象cat來(lái)使用了羽历。
//輸入結(jié)果為"Cat"
animal.showName.call(cat,",");
//animal.showName.apply(cat,[]);
call 的意思是把 animal 的方法放到cat上執(zhí)行,原來(lái)cat是沒(méi)有showName() 方法淡喜,現(xiàn)在是把a(bǔ)nimal 的showName()方法放到 cat上來(lái)執(zhí)行秕磷,所以this.name 應(yīng)該是 Cat
c、實(shí)現(xiàn)繼承
function Animal(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}
function Cat(name){
Animal.call(this, name);
}
var cat = new Cat("Black Cat");
cat.showName();
Animal.call(this) 的意思就是使用 Animal對(duì)象代替this對(duì)象炼团,那么 Cat中不就有Animal的所有屬性和方法了嗎澎嚣,Cat對(duì)象就能夠直接調(diào)用Animal的方法以及屬性了.
d、多重繼承
function Class10()
{
this.showSub = function(a,b)
{
alert(a-b);
}
}
function Class11()
{
this.showAdd = function(a,b)
{
alert(a+b);
}
}
function Class2()
{
Class10.call(this);
Class11.call(this);
}
很簡(jiǎn)單瘟芝,使用兩個(gè) call 就實(shí)現(xiàn)多重繼承了當(dāng)然易桃,js的繼承還有其他方法,例如使用原型鏈锌俱,這個(gè)不屬于本文的范疇晤郑,只是在此說(shuō)明call 的用法。說(shuō)了call 贸宏,當(dāng)然還有 apply造寝,這兩個(gè)方法基本上是一個(gè)意思,區(qū)別在于 call 的第二個(gè)參數(shù)可以是任意類型吭练,而apply的第二個(gè)參數(shù)必須是數(shù)組诫龙,也可以是arguments還有 callee,caller.
上面實(shí)在看不懂在下謝了一些小例子
eg://類式繼承
function Super(){
this.colors=["red","blue"];
}
function Sub(){
Super.call(this);//這里利用call更改this指向
}
console.log(Sub)
console.log(Sub.prototype)//空的證明了什么?空的證明了什么?
let sub=new Sub()
console.log(sub.colors)
//call
// function add(a,b){
// alert(a+b)
// }
// function cal(a,b){
// alert(a-b)
// }
// add.call(cal,2,3)
function add1(a,b){
alert(a+b)
}
function cal1(a,b){
add1.call(this,a,b)//this 后面的值是傳入的參數(shù) apply是數(shù)組形式
}
let add11=new cal1(1,2).
組合繼承 原型+類式
// 組合繼承 原型一般都是放的方法
function Parent(age){
this.name = ['mike','jack','smith'];
this.age = age;
}
Parent.prototype.run = function () {
return this.name + ' are both' + this.age;
};
function Child(age){
Parent.call(this,age);//對(duì)象冒充线脚,給超類型傳參 } Child.prototype = new Parent();//原型鏈繼承
var test = new Child(21);//寫(xiě)new Parent(21)也行 alert(test.run());//mike,jack,smith are both21
}
//寄生模式
// 這種繼承方式是把原型式+工廠模式結(jié)合起來(lái)赐稽,目的是為了封裝創(chuàng)建的過(guò)程叫榕。
function create(o){
var f= obj(o);
f.run = function () {
return this.arr;//同樣浑侥,會(huì)共享引用
};
return f;
}
//寄生式組合
function obj(o){
function F(){}
F.prototype = o;
return new F();
}
function create(parent,test){
var f = obj(parent.prototype);//創(chuàng)建對(duì)象
f.constructor = test;//增強(qiáng)對(duì)象
}
function Parent(name){
this.name = name;
this.arr = ['brother','sister','parents'];
}
Parent.prototype.run = function () {
return this.name;
};
function Child(name,age){
Parent.call(this,name);
this.age =age;
}
inheritPrototype(Parent,Child);//通過(guò)這里實(shí)現(xiàn)繼承
var test = new Child('trigkit4',21);
test.arr.push('nephew');
alert(test.arr);//
alert(test.run());//只共享了方法
var test2 = new Child('jack',22);
alert(test2.arr);//引用問(wèn)題解決