The extend in backbone and underscore

Source: Internet
Author: User

Always confuse the extend in these two libraries, so write it down.

The extend in backbone implements inheritance:

1   //Helper function to correctly set up the prototype chain for subclasses.2   //Similar to ' goog.inherits ', but uses a hash of prototype properties and3   //class properties to be extended.4   varExtend =function(Protoprops, staticprops) {5     varParent = This;6     varChild ;7 8     //The constructor function for the new subclass was either defined by9     //(the "constructor" property is in your ' extend ' definition), or defaultedTen     //By us to simply call the parent constructor. One     if(Protoprops && _.has (Protoprops, ' constructor ')) { AChild =Protoprops.constructor; -}Else { -Child =function(){returnParent.apply ( This, arguments); }; the     } -  -     //Add Static properties to the constructor function, if supplied. - _.extend (Child, parent, staticprops); +  -     //Set The prototype chain to inherit from ' parent ', without calling +     //' Parent ' s constructor function and add the prototype properties. AChild.prototype =_.create (Parent.prototype, protoprops); atChild.prototype.constructor =Child ; -  -     //Set A convenience property in case the parent's prototype is needed -     //later. -child.__super__ =Parent.prototype; -  in     returnChild ; -   }; to  +   //Set up inheritance for the model, collection, router, view and history. -Model.extend = Collection.extend = Router.extend = View.extend = History.extend = Extend;
View Code

The extend in underscore enables shallow replication:

1_.extend =Createassigner (_.allkeys);2 3 varCreateassigner =function(Keysfunc, undefinedonly) {4     return function(obj) {5       varLength =arguments.length;6       if(Length < 2 | | obj = =NULL)returnobj;7        for(varindex = 1; Index < length; index++) {8         varSource =Arguments[index],9Keys =Keysfunc (source),TenL =keys.length; One          for(vari = 0; I < L; i++) { A           varKey =Keys[i]; -           if(!undefinedonly | | obj[key] = = =void0) Obj[key] =Source[key]; -         } the       } -       returnobj; -     }; -};
View Code

The extend in backbone and underscore

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.