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