javascript - Accessing variables in the parent class -
i've been stumped on problem few hours now, , can't seem find solution. have class extends parent class, cannot seem access variables declared in parent's constructor.
i borrowed inheritance technique. uses "extend" function create subclass:
//in functions.js function extend(base, sub, methods) { sub.prototype = object.create(sub.prototype); sub.prototype.constructor = sub; sub.base = base.prototype; for(var name in methods) { sub.prototype[name] = methods[name]; } return sub; }
i create class called stimulus serves function:
//in classes.js function stimulus(module_id, unit_id, attributes) { this.attributes = attributes; this.module_id = module_id; this.unit_id = unit_id; //create other class variables based on this.attributes, this.module_id, , this.unit_id } stimulus.prototype = { _getstimulus: function() { //retrieve database } //other functions here }
and have subclass. technique use create borrowed above link:
//in classes.js imagestimulus = (function() { var $this = function(module_id, unit_id, attributes) { $this.base.constructor.call(this, module_id, unit_id, attributes); }; extend(stimulus, $this, { initialize: function() { this.fixation_cross = this.attributes['fixation cross'] ? this.attributes['fixation cross'] : false; //do other stuff } //other functions here }); return $this; })();
it seems straightforward enough. however, in main script, when try run this, create object , try run initialize()
function , falls apart:
//in main.js var stimulus_objects = []; for(var = 0; < somelimit; i++) { //module_id passed directly function var unit_id = //some source; var stimulus_attributes = //some source; stimulus_objects[i] = new imagestimulus(module_id, unit_id, stimulus_attributes); stimulus_objects[i].initialize(); }
if check console see says
uncaught typeerror: cannot read property 'fixation cross' of undefined
and corresponds line in imagestimulus.initialize() try call on this.attributes['fixation cross']
.
it seems going wrong in making stimulus
prototype of imagestimulus
, because imagestimulus.initialize()
cannot access this.attributes
variables created in constructor stimulus
class.
does else see error?
i have decent amount of oop programming in java, c++, , php, first attempt @ javascript oop, , feel i'm making simple mistake.
edit: solved problem... somehow.
so seems there trivial solution. stimulus
function never being called, , should have been called on line $this.base.constructor.call()
. in stimulus.prototype
object added constructor: stimulus
, stimulus
being called properly. seems odd had (shouldn't stimulus()
it's own constructor?), works!
stimulus.prototype = { constructor: stimulus, _getstimulus: function() {...
does know why occurred , why fix worked? i'm trying understand did.
i it. you're replacing entire prototype, killing constructor. setting constructor explicitly, putting back. alternative set prototype method directly rather setting entire prototype.
for fiddle useful, bring console , set break point before clicking run. http://jsfiddle.net/x2v7wv6j/
//in functions.js function extend(base, sub, methods) { sub.prototype = object.create(sub.prototype); sub.prototype.constructor = sub; sub.base = base.prototype; for(var name in methods) { sub.prototype[name] = methods[name]; } return sub; } //in classes.js function stimulus(module_id, unit_id, attributes) { this.attributes = attributes; this.module_id = module_id; this.unit_id = unit_id; //create other class variables based on this.attributes, this.module_id, , this.unit_id } stimulus.prototype._getstimulus = function() { //retrieve database } //other functions here //in classes.js imagestimulus = (function() { var $this = function(module_id, unit_id, attributes) { $this.base.constructor.call(this, module_id, unit_id, attributes); }; extend(stimulus, $this, { initialize: function() { this.fixation_cross = this.attributes['fixation cross'] ? this.attributes['fixation cross'] : false; //do other stuff } //other functions here }); return $this; })(); var foo = function (module_id) { var stimulus_objects = []; var somelimit = 10; for(var = 0; < somelimit; i++) { //module_id passed directly function var unit_id = "some source";//some source; var stimulus_attributes = "some source"; //some source; stimulus_objects[i] = new imagestimulus(module_id, unit_id, stimulus_attributes); stimulus_objects[i].initialize(); } } foo(1);
Comments
Post a Comment