javascript - Default arguments in functions which defined in object -
here code:
mymodel = { get: function(key, model) { if(typeof(model) === 'undefined') { // model = model.get(); // } // return model.data[key]; }, getall: function(model) { if(typeof(model) === 'undefined') { model = model.get(); } return model.data; }, save: function(data, model) { if(typeof(model) === 'undefined') { model = model.get(); } model.save(data); }, //... }
the a
part repeats.
is there way more beautiful?
something get: function(key, model = model.get())
;
from mdn, not work in many browsers.
function setbackgroundcolor(element, color = 'rosybrown') { element.style.backgroundcolor = color; } setbackgroundcolor(somediv); // color set 'rosybrown' setbackgroundcolor(somediv, undefined); // color set 'rosybrown' setbackgroundcolor(somediv, 'blue'); // color set 'blue'
you can make function in object, keeping code dry
getmodel: function(model){ return model || model.get(); }
now use following in blocks need model.
var test_model = this.getmodel(model);
Comments
Post a Comment