javascript物件導向

來源:互聯網
上載者:User

標籤:

1.Javascript物件導向的概念
(1).一切事物皆對象
(2).對象具有封裝和繼承特性
(3).對象與對象之間使用訊息通訊,各自存在資訊隱藏
Javascript是一種基於對象(object-based)的語言,你遇到的所有東西幾乎都是對象。但是,它又不是一種真正的物件導向編程(OOP)語言,因為
它的文法中沒有class(類)
2.基於類的物件導向和基於原型的物件導向方式比較
在基於類的物件導向方式中,對象(object)依靠 類(class)來產生。而在基於原型的物件導向方式中,對象(object)則是依靠 構造器
(constructor)利用 原型(prototype)構造出來的。舉個客觀世界的例子來說明二種方式認知的差異。例如工廠造一輛車,一方面,工人必須參照一
張工程圖紙,設計規定這輛車應該如何製造。這裡的工程圖紙就好比是語言中的 類 (class),而車就是按照這個 類(class)製造出來的;另一方面,工
人和機器 ( 相當於 constructor) 利用各種零組件如發動機,輪胎,方向盤 ( 相當於 prototype 的各個屬性 ) 將汽車構造出來。
3. 物件導向的特點:
封裝、繼承和多態。這裡先詳細說明這幾個特點,後面幾個部分分別在JavaScript中實現這些特點,從而實現完整的物件導向類比。 ?
封裝:封裝就是把各種方法和變數合并到一個類,用這個類代表某個對 象為完成一定的任務所能儲存的範圍以及它能執行的操作。封裝隱藏了方法執行
的細節。 ?
繼承:繼承就是根據現有類的方法和成員變數產生新的類的功能。 ?
多態:多態就是對象隨著程式執行而使其形式發生改變的能力
4.基礎資料型別 (Elementary Data Type):
Number, String, Boolean, Function, Object, Array, null, undefined
注意null和undefined的區別。
5.繼承的實現
(1).對象冒充法
<script type="text/javascript" language="javascript">
function classA(sColor){
this.color=sColor;
this.sayColor=function(){
alert(this.color)
}
}
function classB(sColor,sName){
this.method=classA();// ClassA的建構函式成為ClassB的方法
this.method=(sColor); // 使用ClassA的建構函式,ClassB會收到ClassA的建構函式中定義的屬性和方法
delete this.method; // 刪除ClassA的引用
this.name=sName;
this.sayName=function(){
alert(this.name)
}
}
var obj= new ClassB("blue", "Nicholas");
obj.sayColor();
obj.sayName();
alert(obj instanceof ClassA); // 返回false
alert(obj instanceof ClassB); // 返回true
</script>
以上,ClassA為建構函式,其實也只是一個函數,所以ClassA的構造子數成為了ClassB的一個方法,然後調用該方法,在ClassB中引入了ClassA()構造
函數中定義的屬性和方法。最後刪除對ClassA的引用。這樣一來,相當於ClassA建構函式中定義的屬性和方法成為了ClassB中定義的屬性及方法。
對於instanceof方法,在使用對象冒充繼承時,如果使用instanceof <基類名>是返回false的。所以對象冒出繼承是假繼承法
冒充方法只適合所有的屬性及方法在建構函式中進行定義的類
(2).call()和apply()方法
● call方法與經典的對象冒充方法最相似,它第一個參數用作this的對象,表示需要被繼承的對象,其它參數都直接傳遞給基類建構函式的。
<script type="text/javascript" language="javascript">
function ClassA(sColor) {
this.color = sColor;
this.sayColor = function() { alert(this.color); };
}
function ClassB(sColor, sName) {
//this.newMethod = ClassA;
//this.newMethod(sColor);
//delete this.newMethod;
ClassA.call(this, sColor); // 很簡便,一行相當於上面三行。
this.name = sName;
this.sayName = function () { alert(this.name); };
}
// 使用直接定義對象方法。從相應的對象中繼承。
function sayColor(sPrefix, sSuffix) {
alert(sPrefix + this.color + sSuffix);
}
var obj = new Object();
obj.color ="red";
sayColor.call(obj, ‘The color is ‘, ‘, a very nice color indeed.‘); // 表示obj對象繼承自sayColor.
</script>
● apply方法有兩個參數,分別為this對象(即需要被繼承的對象)和要傳遞給基類建構函式參數的數組。例如:
function ClassB(sColor, sName) {
//this.newMethod = ClassA;
//this.newMethod(sColor);
//delete this.newMethod;
ClassA.apply(this, new Array(sColor)); // 與call不同的是,第二個參數是一個參數數組.
this.name = sName;
this.sayName = function () { alert(this.name); };
}
3.原型鏈繼承
<script type="text/javascript">
function ClassA(sColor) {
this.color = sColor;
}
ClassA.prototype.sayColor = function() { alert(this.color); }; // 原型方法
//如果需要繼承ClassA中的原型方法,需要在子類中使用以下語句: ClassB.prototype = new ClassA();
function ClassB(sColor, sName) {
ClassA.call(this, sColor); // 繼承了ClassA中的構造方法

this.name = sName;
this.sayName = function() { alert(this.name); };
}
ClassB.prototype = new ClassA(); // 繼承了ClassB中的原型方法

var objB = new ClassB("blue", "Nicholas");
objB.sayColor();
objB.sayName();
alert(objB instanceof ClassA); // 返回true
alert(objB instanceof ClassB); // 返回true
</script>
一般來說,在Javascript中,繼承一個類,對象冒充及原型鏈方式都會被混合在一起使用。使用對象冒充繼承建構函式的屬性,使用原型鏈繼承
prototype對象的方法。

javascript物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.