談談JS中的物件導向

來源:互聯網
上載者:User

標籤:des   style   blog   color   java   使用   os   io   

請先看看下面這段代 

1 <script src="jquery.js"></script>2 <script type="text/javascript">3  /**
  4      * Object.create() 最近才添加進了ECMAScript第5版規範,有些瀏覽器不支援  5      * 這裡類比一個Object.create方法解決相容性問題  6      * Object.create : 該方法只有一個參數,即原型對象,返回一個新對象  7      * 這個新對象的原型就是傳入的參數。即傳入一個對象,返回一個繼承了這個對象的新對象  8      */  9  10     if(typeof Object.create != "function") { 11         Object.create = function (o) { 12             function F() {} 13             F.prototype = o; 14             return F(); 15         } 16     } 17  18     /** 19      * 簡單明了的GUID 產生器,用於自動化的產生ID,為儲存執行個體記錄增加ID支援 20      */ 21     Math.guid = function(){ 22         return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx‘.replace(/[xy]/g, function( c ) { 23             var r = Math.random() * 16 | 0; 24             var v = c == ‘x‘ ? r : (r&0x3|0x8); 25             return v.toString(16); 26         }).toUpperCase(); 27     }; 28  29     /** 30      * creat a object 從這裡開始,建立一個物件模型,注意,這裡的Model是掛載到Object基類下,比Function、Array等類的層級高 31      */ 32     var Model = { 33         inherited : function () {}, 34         created : function () {}, 35  36         prototype : { 37             init : function () {} 38         }, 39         //這個函數會返回一個新對象,這個對象繼承自Model對象 40         //我們使用它來建立新模型 41         create : function () { 42             var object = Object.create(this); 43             object.parent = this; 44             object.prototype = object.fn = Object.create(this.prototype); 45  46             object.created(); 47             this.inherited(); 48             this.inherited(object); 49  50             return object; 51         }, 52         //這個函數會初始化返回一個新對象 53         //它繼承自Model.prototype,比如Model對象的一個執行個體 54         init : function () { 55             var instance = Object.create(this.prototype); 56             instance.parent = this; 57             instance.init.apply(instance, arguments); 58  59             return instance; 60         }, 61         //這個方法為Model對象擴充多個屬性 62         extend : function (o) { 63             var extented = o.extended; 64  65             $.extend(this, o); 66             if(extented){ 67                 extented(this); 68             } 69         }, 70         //這個方法為Model對象擴充執行個體屬性 71         include : function(o){ 72             var included = o.included; 73  74             $.extend(this.prototype, o); 75             if(included){ 76                 included(this); 77             } 78         } 79     }; 80  81     //我們需要保持記錄的持久化,可以實現儲存一個新建立的執行個體時,就將它添加進這個對象 82     //當刪除執行個體時,就將它從對象中刪除 83     Model.records = {}; 84     Model.include({ 85         newRecord : true, 86         //create a isntance 87         create : function () { 88             this.newRecord = false; 89             this.parent.records[this.id] = this; 90         }, 91         //delete a instance 92         destroy : function () { 93             delete this.parent.records[this.id]; 94         }, 95         //update a instance existed 96         update : function () { 97             this.parent.records[this.id] = this; 98         }, 99         //save a instance100         save : function () {101             this.newRecord ? this.create() : this.update();102         },103         init: function(atts) {104             /* ... */105         },106         load: function(attributes){107             /* ... */108         }109 110     });111     /**112      * 增加ID支援113      */114     Model.extend({115         //search a instance by id116         find : function (id){117             return this.records[id];118         }119         create : function(){120             if ( !this.id ) {121                 this.id = Math.guid();122             }123             this.newRecord = false;124             this.parent.records[this.id] = this;125         }126     });127 128     /**129      * for test130      */131     var Asset = Model.create();132     var User = Model.create();133 134     var user = User.init();135 136     var asset = Asset.init();137     asset.name = "same, same";138     asset.id = 1;139     asset.save();140 141     var asset2 = Asset.init();142     asset2.name = "but different";143     asset2.id = 2;144     asset2.save();145 146 147 </script>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.