Friday, 15 April 2011

oop - How to obtain the variable name which holds an object instance from within the object : Javascript -



oop - How to obtain the variable name which holds an object instance from within the object : Javascript -

i not able figure out dynamic/general way accomplish this. want name of variable holds initiated object within object functions.

its kind of hard explain without example. dont know if there "technical/programmatic" name such procedure.

take example

i initiate object as..

var jtest = new boom('hi');

i have objects constructor as..

function boom(message){ console.log(message+' - '+objname); // want in value of 'objname' 'jtest' // want know dynamically name of variable going hold object instance (in case 'jtest'). // when create html elements object // , later have events way access object instance // within event functions. // eg this.ref = document.createelement('div'); this.ref.setattribute('style','display:block;width:100px;height:100px;background:#f00'); this.ref.setattribute('data-obj',objname); // <- obj name contains jtest or whatever name object instance stored // if saved name 'jtest' within element attr ref later this.body.appendchild(this.ref); this.ref.addeventlistener('click',(function(event){ // - on here refers element not object instance jtest or other forsaken name jtest.dance(); // want run object property called dance() // how dynamically...? // need know name of variable holds object instance // can phone call property // attr "data-obj" value 'jtest' help ! }),true); }

i want know literal name of variable(in case 'jtest') contains reference newly created object instance within of object.

so can set identifier, class (.jtest) html elements create within of object.

so when have events on html elements know instance created them , can access appropriate object functions.

hope understandable. seek update description new ideas explain myself. suggest me other ways solve problem 'a way know , phone call object instance created element within elements event(hover / click) function'.

i think should looking @ question : determine name of javascript object instance's class

but, object jtest not "name", pointer object. big question here should be: why want know name of pointer instance you're working with? pass instance you're working object this. (see: http://www.quirksmode.org/js/this.html )

javascript oop object instance instance-variables

No comments:

Post a Comment