Talking about object-oriented in JS

Source: Internet
Author: User
Tags guid generator

Please take a look at the following generation

1 <script src= "Jquery.js" ></script>2 <script type= "Text/javascript" >3  /**
4 * Object.create () has only recently been added to the ECMAScript 5th Edition specification, some browsers do not support5 * Here simulates a object.create method to resolve compatibility issues6 * Object.create: The method has only one parameter, that is, the prototype object, which returns a new object7 * The prototype of this new object is the parameter passed in. That is to pass in an object that returns a new object that inherits the object.8 */9 Ten if(typeofObject.create! = "function") { OneObject.create =function(o) { A functionF () {} -F.prototype =o; - returnF (); the  } -  } -  - /** + * Simple and straightforward GUID generator for automating the build ID, adding ID support for saving instance records - */ +Math.guid =function(){ A return' Xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx '. Replace (/[xy]/g,function(c) { at varR = Math.random () * 16 | 0; - varv = c = = ' x '? R: (r&0x3|0x8); - returnV.tostring (16); - }). toUpperCase (); -  }; -  in /** - * creat A object from here, create an objects model, note that the model here is mounted under the object base class, higher than the class level of function, array, etc. to */ + varModel = { -Inherited:function () {}, theCreated:function () {}, *  $ prototype: {Panax NotoginsengInit:function () {} -  }, the //This function returns a new object that inherits from the model object + //we use it to create new models ACreate:function () { the varObject = Object.create ( This); +Object.parent = This; -Object.prototype = Object.fn = Object.create ( This. prototype); $  $ object.created (); -  This. inherited (); -  This. Inherited (object); the  - returnobject;Wuyi  }, the //This function initializes the return of a new object - //it inherits from Model.prototype, such as an instance of the model object WuInit:function () { - varInstance = Object.create ( This. prototype); AboutInstance.parent = This; $ instance.init.apply (instance, arguments); -  - returninstance; -  }, A //This method extends multiple properties for the model object +Extend:function(o) { the varextented =o.extended; -  $$.extend ( This, O); the if(extented) { theExtented ( This); the  } the  }, - //This method extends the instance properties for the model object inInclude:function(o) { the varincluded =o.included; the  About$.extend ( This. prototype, o); the if(included) { theIncluded This); the  } +  } -  }; the Bayi //we need to keep the record persistent and add it to this object when we save a newly created instance . the //When you delete an instance, it is removed from the object theModel.records = {}; - Model.include ({ -NewRecord:true, the //Create a isntance theCreate:function () { the  This. NewRecord =false; the  This. parent.records[ This. id] = This; -  }, the //Delete a instance theDestroy:function () { the Delete  This. parent.records[ This. Id];94  }, the //update a instance existed theUpdate:function () { the  This. parent.records[ This. id] = This;98  }, About //Save a instance -Save:function () {101  This. NewRecord? This. Create (): This. Update ();102  },103Init:function(atts) {104 /* ... */ the  },106Loadfunction(attributes) {107 /* ... */108  }109  the  });111 /** the * Add ID support113 */ the Model.extend ({ the //search A instance by ID theFind:function(ID) {117 return  This. Records[id];118  }119 create:function () { - if (!this.id) {121 this.id = Math.guid ();122  }123 This.newrecord = false;124 This.parent.records[this.id] = this; the  }126  });127  - /**129 * for Test the */131 varAsset =model.create (); the varUser =model.create ();133 134 varuser =user.init ();135 136 varAsset =asset.init ();137Asset.name = "Same, Same";138Asset.id = 1;139 Asset.save (); $ 141 varAsset2 =asset.init ();142Asset2.name = "but different";143Asset2.id = 2;144 Asset2.save ();145 146 147</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.