- 首先明確this在javascript當(dāng)中的指的是調(diào)用者,在java中指的是當(dāng)前對(duì)象,這兩者有本質(zhì)區(qū)別的
- JavaScript中沒(méi)有類的概念,繼承描述對(duì)象之間的關(guān)系,繼承關(guān)鍵在于子類獲取父類的成員及方法的方式
1. 對(duì)象冒充
function Parent(name)
{
this.name = name;
this.sayHello = function()
{
alert(this.name)
}
}
function Child(name, age)
{
//3行關(guān)鍵代碼 此三行用于獲取父類的成員及方法
//用子類的this去冒充父類的this,實(shí)現(xiàn)繼承
//父類Parent中的this.name、sayHello,分別成為了子類的成員、子類的方法
this.method = Parent;
//接收子類的參數(shù) 傳給父類
this.method(name);
//刪除父類
delete this.method;
//此后的this均指子類
this.age = age;
this.sayWorld = function()
{
alert(age);
}
}
var parent = new Parent("張三");
var child = new Child("李四", 20);
parent.sayHello();
child.sayHello();
child.sayWorld();
2. call 方法方式
//call 方法是 Function 對(duì)象中的方法,因此我們定義的每個(gè)函數(shù)都擁有該方法。
//可以通過(guò)函數(shù)名來(lái)調(diào)用call 方法,call 方法的第一個(gè)參數(shù)會(huì) 被傳遞給函數(shù)中的this,
//從第 2 個(gè)參數(shù)開(kāi)始,逐一賦值給函數(shù)中的參數(shù)
//call方法的使用
function test(str, str2)
{
alert(this.name + ", " + str + ", " + str2);
}
var object = new Object();
object.name = "zhangsan";
//test.call相當(dāng)于調(diào)用了test函數(shù)
//將object賦給了this
test.call(object, "JavaScript", "hello");
//call方法實(shí)現(xiàn)繼承
function Parent(name)
{
this.name = name;
this.sayHello = function()
{
alert(name);
}
}
function Child(name, age)
{
//子類的this傳給父類
Parent.call(this, name);
this.age = age;
this.sayWorld = function()
{
alert(age);
}
}
var parent = new Parent("張三");
var child = new Child("李四", 20);
parent.sayHello();
child.sayHello();
child.sayWorld();
3. apply方法方式
//call 和 apply 都是為了改變某個(gè)函數(shù)運(yùn)行時(shí)的 context (上下文)而存在的,
//是為了改變函數(shù)體內(nèi)部 this 的指向,劫持其他對(duì)象的方法
//call 和 apply二者的作用完全一樣,只是接受參數(shù)的方式不太一樣,apply第二個(gè)參數(shù)是一個(gè)參數(shù)列表
function Parent(name)
{
this.name = name;
this.sayHello = function()
{
alert(name);
}
}
function Child(name, age)
{
Parent.apply(this, new Array(name));
this.age = age;
this.sayWorld = function()
{
alert(age);
}
}
var parent = new Parent("張三");
var child = new Child("李四", 30);
parent.sayHello();
child.sayHello();
child.sayWorld();
4. 原型鏈方式(prototype chain )
//無(wú)法給構(gòu)造函數(shù)傳參數(shù)
function Parent()
{
}
Parent.prototype.name = "張三";
Parent.prototype.sayHello = function()
{
alert(this.name);
}
function Child()
{
}
//子類的原型引用指向父類
Child.prototype = new Parent();
Child.prototype.age = 20;
Child.prototype.sayWorld = function()
{
alert(this.age);
}
var child = new Child();
child.sayHello();
child.sayWorld();
5. 混合方式(克服了原型璉的弊端)
//將對(duì)象方法拿到對(duì)象的外面定義,封裝對(duì)象屬性
function Parent(name)
{
this.name = name;
}
Parent.prototype.sayHello = function()
{
alert(this.name);
}
function Child(name, age)
{//子類的this傳給parent
Parent.call(this, name);
this.age = age;
}
//子類的原型引用指向父類對(duì)象
Child.prototype = new Parent();
Child.prototype.sayWorld = function()
{
alert(this.age);
}
var child = new Child("李四", 30);
child.sayHello();
child.sayWorld();
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。