javascript running an object method inside objects constructor -



javascript running an object method inside objects constructor -

this question has reply here:

how access right `this` / context within callback? 4 answers

i trying implement own object in javascript. using protype method add together the objects , it's methods. following

function mycustomobject(){ this.attr1 = value; //more atributes here. works fine this.options = { imagefilenames: ['image1.png', 'image2.png',...], baseurl:'url;' }; this.objectmethod(); } mycustomobject.prototype.objectmethod = function (){ //..... method's body here. var url = this.options.url; this.options.imagefilenames.foreach(function (filename, index){ var src = url+'/'+filename; imageobj = new image(); imageobj.src = src; imageobj.load = function (){ image = new kinetic.image({ x:0, y:0, id: filename, width: imageobj.width, height: imageobj.height, image:imageobj }); this.imageobjects[index] = imageobj; this.teethimages[index] = image; }; if (this.imageobjects.length === this.options.imagefilenames.length){ this.positionimages(); this.createlinegroups(); this.createlabelgroups() this.createstage(); this.drawonscreen(); } }); }

my problem when trying utilize keyword within foreach function options object , not mycustomobject. how can bypass behaviour?

simple solution :

var obj = this, url = obj.options.url; obj.options.imagefilenames.foreach(function (filename, index){ // utilize obj instead of here

you'll able utilize es6 arrow notation :

obj.options.imagefilenames.foreach((filename, index) => { // external unchanged

(this works in firefox , chrome)

javascript javascript-objects

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -