class - Javascript Inheritance and the words "this" and "that" -
this question has answer here:
i have class use methods event listeners, don't use this that in methods.
this base class:
function modal(){ var = this; this.opened = false; this.open = function(){ that.opened = true; var id = this.id; // more code }; this.close = function () { if (that.opened) // something; }; } this inherited class:
function modalbook(){ var = this; this.open = function(){ that.opened = true; var id = this.id; // more code }; } modalbook.prototype = new modal(); var modalbook = new modalbook(); now value of modal.opened true when modal opened when it's closed value false.
on debugging on line of this.close, saw that instance of modal, , modalbook instance of modalbook.
so question is: how preserve value of this in both methods modalbook?
what's happening there modalbook.prototype=new modal() modalbook 'inheriting' modal. variables belong each constructor in scopes , properties overwritten. so:
thatinmodalstay locally inmodalthatinmodalbookinstante ofmodalbook- but
open()method existmodalbook.open()(you're overwritting other one)thatinstance ofmodalbook, otherthatdoesn't exist in scope.
i think have design problem. maybe use properties different name thats.
Comments
Post a Comment