javaScript 物件導向開發執行個體

來源:互聯網
上載者:User

標籤:我愛我家   span   lis   nts   模組化   elf   port   div   ret   

javaScript 物件導向開發執行個體

這個是結合require的模組化開發,首先建立建構函式

//test.js
1 function Test(lists) { 2 var config={ 3 name:lists.name, 4 sex:lists.sex 5 }; 6 this.init(config) 7 } 8 Radio.prototype = { 9 init: function(config) { 10 var self=this; 11 self.initContent(config);12 }13 initContent:function(config){14 var self=this;15 //do somethings16 17 },18 //可以在外部直接調用該方法19 getCurrentState:function() {20 var self=this; 21 var selects =‘我愛我家‘;22 return selects; 23 },24 }25 module.exports = Test;

在另一個檔案調用該函數:

var Test=require("./test.js");   var example =new Test({      name:‘剛二代‘,      sex:‘boy‘});  //調用getCurrentState方法example.getCurrentState()//我愛我家

 

(1)將方法綁定在對象上,執行個體化對象的時候,該對象就擁有了這些方法;

(2)可以直接調用執行個體化對象的方法;

更詳細的詳見 javaScript物件導向與原型

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.