javascript - Very odd behavior when returning a prototypical function from a module function -
i'm writing boilerplate new site , found myself interesting problem. want set module returns interface can extended. using prototypical inheritance should make easy task, right? though returning new instance of function, isn't extensible. have feeling monday morning mixup...
var somemodule = somemodule || (function($) { /*initalize page*/ init(); ///////////////////// // public methods // ///////////////////// var api = function() { } api.prototype.example = function() { alert("hi!"); } ////////////////////// // private methods // ////////////////////// function init() { ... } return new api(); })(jquery); somemodule.prototype.newfunct = function() { ... } //causes exception because somemodule object, not function typeof(somemodule); // "object"
to answer own question, here's way prototypical inheritance working design. question becomes, design in first place?
var somemodule = somemodule || (function($) { /*initalize page*/ init(); ///////////////////// // public methods // ///////////////////// var api = function() { ... } api.prototype.example = function() { alert("hi!"); } ////////////////////// // private methods // ////////////////////// function init() { ... } var public = new api(); return { extend: api.prototype, api:public } })(jquery); somemodule.extend.newfunction = function(){ console.log('i'm altering prototype!'); }
Comments
Post a Comment