JavaScript物件導向編程入門

來源:互聯網
上載者:User

標籤:傳參   建立   java   ret   turn   編程   父類   bsp   表示   

來源極客網

 1 function Person() { 2     var _this = {} //建立一個空的對象,接著我們利用這個"空的對象"承載Person的屬性和方法 3     _this.sayHello = function(){ 4         alert("PHello"); 5     } 6     return _this;  7 } 8  9 function Teacher_1() {10     var _this = Person();//注意這裡的文法小括弧11     return _this;12 }13 14 //示範覆寫父類中的方法文法15 function Teacher_2() {16     var _this = Person();17     _this.sayHello = function () {18         alert("Thello");19     }20     return _this;21 }22 //示範子類怎樣調用父類的方法23 function Teacher() {24     var _this = Person();25     var surperSay = _this.sayHello;     //1.通過"賦值"的代碼思想進行處理26     _this.sayHello = function () {27         surperSay.call(_this) //2.通過call()方法進行強行調用28         alert("Thello");29     }30     return _this;31 }32 33 //調用我們建立對象Teacher34  var t = Teacher();35 t.sayHello();//調用到我們在Person中聲明的sayHello

自己隨便定義html運行一下上面的代碼,感受一下

 1 //示範用一個函數將一個function包裹起來,然後執行它 2 //第5行增加傳參的情況怎樣處理 3 (function(){ 4     var n = "ime"; 5     function Person(name) { 6         var _this = {} //建立一個空的對象,接著我們利用這個"空的對象"承載Person的屬性和方法 7         _this._name = name; 8         _this.sayHello = function(){ 9             alert("PHello" + this._name + ":"+ n);10         }11         return _this; 12     }13     window.Person = Person; //表示將函數Person"掛"給全家變數,14 }());//大括弧右邊的一對小括弧,是必須有的,表示自運行15 //示範了16 function Teacher(name) {17     var _this = Person(name);18     var superSay = _this.sayHello;19     _this.sayHello = function () {20         superSay.call(_this);21         alert("Thello" + _this._name);22     }23     return _this;24 }25 var t = Teacher("iwen");26 t.sayHello();

 

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.