Javascript / jQuery Prototype inheritance extend defaults -
is there way how extend default options multiple prototype based javascript constructor functions?
i'm trying options object in events object, possible?
var website = function(options){ this.options = $.extend({}, this.defaults, options); } website.prototype = { constructor : website, defaults: { navigation: $('#navigation'), footer : $('#footer') } init: function(){ } }; var events = function(){ this.init() } events.prototype = { constructor: website, init:function(){ console.log(...) } }
this significant rewrite, i'd suggest storing defaults in outside variable both "classes" can share:
var app = (function ($) { var defaults = { navigation: "nav", footer : "foot" }; var website = function (options) { this.options = $.extend({}, defaults, options); this.echo = function (message) { console.log(message); }; }; var events = function (options) { this.options = $.extend({}, defaults, options); this.echotwo = function (message) { console.log(message + message); }; }; return { website: website, events: events }; }(jquery)); var website = new app.website({navigation: "changed"}); website.echo(website.options.navigation + ' '+ website.options.footer); // "changed foot" var events = new app.events({footer: "bar"}); // events.echotwo(events.options.navigation + ' '+ events.options.footer); // "nav barnav bar" this way, can set shared options through variable available in scope of wrapping, self-invoking function. structure , nomenclature little fuzzy, general idea clear.
edit
i've restructured things allow each "class" have it's own options set on instantiation. same set defaults app well.
Comments
Post a Comment