backbone.js - why it use return parent.apply(this, arguments) in model -


i don't know why use return parent.apply(this, arguments) inherit ?

// constructor function new subclass either defined // (the "constructor" property in `extend` definition), or defaulted // call parent constructor.  if (protoprops && _.has(protoprops, 'constructor')) {   child = protoprops.constructor; } else {   child = function(){      return parent.apply(this, arguments);    }; }  // add static properties constructor function, if supplied. _.extend(child, parent, staticprops);  // set prototype chain inherit `parent`, without calling // `parent` constructor function. var surrogate = function() {    this.constructor = child;  }; surrogate.prototype = parent.prototype; child.prototype = new surrogate; 

this strictly related how javascript object extension , inheritance working. in javascript extend object new properties have extend it's base prototype.

here small snippet widespread , and elegant inheritance method. there other prototypal inheritance methods, best know crockford's inheritance method (see below).

// classical (the best use) surrogate = function () {     parent.call(this);     this.type = 'child element';     //... other properties };  surrogate.prototype = object.create(parent.prototype); surrogate.prototype.constructor = surrogate;  surrogate.prototype.copy = function (source) {      parent.prototype.copy.call(this, source);             return this; }; 

crockford inheritance method:

function object(o) {     function f() {}     f.prototype = o;     return new f(); } 

so coming question. difference between apply , call first 1 require array of arguments parameter. apply, can use array literal, example,

fun.apply(this, ['eat', 'bananas']) 

or array object, example:

fun.apply(this, new array('eat', 'bananas')). 

for thorough explanation check article:

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/function/apply

the extend function same thing: extend parent object parameters.

return 

returns object instance on method has been called, , used methods chaining. call multiple methods @ once on same instantiated object:

var surrogate = new surrogate(); surrogate.copy().delete(); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -