javascript對象學習筆記,javascript學習筆記

來源:互聯網
上載者:User

javascript對象學習筆記,javascript學習筆記

多的不說,直接上練習例子:

1.對象的屬性、建立、建構函式、方法

<html xmlns="http://www.w3.org/1999/xhtml"><body><script type="text/javascript">//函數:用作對象的方法function Rectangle_area() {return this.width * this.height;}function Rectangle_perimeter() {return this.width * 2 + this.height * 2;}function Rectangle_set_size(w, h) { this.width = w; this.height = h;}//object_Rectangle 的建構函式function object_Rectangle(w, h) {//初始化對象的屬性this.width = w;this.height = h;//定義對象的方法this.area = Rectangle_area;this.perimeter = Rectangle_perimeter;this.set_size = Rectangle_set_size;}//建立object_Rectangle對象,調用它的方法var r = new object_Rectangle(3, 4);var a = r.area();document.write(a);</script></body></html>

結果: 12


2.原型對象和繼承

<html xmlns="http://www.w3.org/1999/xhtml"><body><script type="text/javascript">//函數:用作對象的方法function Rectangle_area() {return this.width * this.height;}function Rectangle_perimeter() {return this.width * 2 + this.height * 2;}function Rectangle_set_size(w, h) { this.width = w; this.height = h;}//object_Rectangle 的建構函式function object_Rectangle(w, h) {//初始化對象的屬性this.width = w;this.height = h;//定義對象的方法this.area = Rectangle_area;this.perimeter = Rectangle_perimeter;this.set_size = Rectangle_set_size;}
//建立原型對象new object_Rectangle(0,0);
//定義一個常量: 所有<span style="font-family: Arial, Helvetica, sans-serif;">object_Rectangle對象共有的屬性</span>object_Rectangle.prototype.d = 5;object_Rectangle.prototype.return = function () { return this.width * this.d;}//建立object_Rectangle對象,調用它的方法var r = new object_Rectangle(3,4);var a = r.area();var b = r.return();document.write(a + " " + b);var name = "";</script></body></html>

結果:12 15

3.作為關聯陣列的對象

   object.property   等價於 object[ "property" ]

   二者存在區別:

           前者是靜態訪問,“.”訪問的是標識符,不是任何類型,程式無法操作它;

       後者可以動態訪問:

eg:

var address= "";for(var i = 0, i <4, i++){    address += object_Rectangle["address" + i] + "\n";}
讀取屬性
address0,address1,address2,address3


Javascript 中的類與對象

JavaScript 是物件導向的語言,引用資料類型都是對象,包括函數也是對象,同時還可以通過 Object 對象自訂對象。
但是,和其他物件導向語言(如 Java 等進階語言)比,也有很大差異,JS 中沒有類或介面的概念,即不能直接定義抽象的類,也不能直接實現繼承。不過,為了編程的方便,我們可以在 JS 中類比類和繼承的行為。
建立對象執行個體或類(雖然沒有類的概念,但是可以把用於建立新對象的對象看作類),可以通過建構函式來實現,建構函式就是具有一系列屬性和行為作為函數體的函數,可以通過函數參數傳入值。它就相當於 Java 中類的建構函式,需要時可以通過 var instanceObj = new ConstructorFunc(para1,para2,...) 來建立對象(執行個體)。
JS 的對象中還有一個重要概念,即原型。每個對象都有原型,原型也是一個對象,可以看做是建構函式的映像,是建立執行個體的模型。對象(類)的屬性 prototype 即是對原型對象的引用,建立執行個體後,也有屬性 __proto__ 指向原型對象,但該屬性是隱含的。
由於不斷創造新對象(執行個體),一級一級的傳遞原型對象,即可構成原型鏈。通過原型鏈,即可實現繼承。首先將父類對象的執行個體給子類的原型 ChildCons.prototype = new ParentCons(),再在子類建構函式中調用父類建構函式將繼承的屬性初始化。繼承的變通方法還有很多,可以參考一些資料。
 
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.