jquery - Javascript Prototype Pattern - Feedback -
i'm switching using javascript revealing module pattern , have below seems work. want know if i'm doing correct , follow best practices. example, way i'm preserving 'this' state , calling init function in constructor correct?
var testapp = function(){ //kick off this.init(); }; testapp.prototype = { getusers: function(callback){ //do stuff }, buildusertable: function(data){ //do stuff }, refreshtable: function(){ //example this.getusers(); }, init: function(){ //preserve 'this' var instance = this; //callback + init this.getusers(function(data){ instance.buildusertable(data); }); $('.formsection .content').hide(); $('.formsection .content:first').slidedown('slow').addclass('selected'); } }; window.onload = function () { var form = new testapp(); };
you're overriding prototype completely. can't deal inheritance way.
since {}
object implicitly inheriting object
nothing else.
inheritance looks this:
function a() {}; function b() {}; b.prototype = new a(); var b = new b(); console.log(b instanceof a); // "true"
b
inherits a
, object
.
if do:
b.prototype = { foo: function () {} }; var b = new b(); console.log(b instanceof a); // "false"
you're not longer inhering a
;
how add functions prototype? use notation:
b.prototype.foo = function () {};
Comments
Post a Comment