neoform Posted March 8, 2007 Share Posted March 8, 2007 I've run into a small problem with multiple instances of objects and their internal functions.. var A = new Object(); A = { var_a : 'hello', func_a : function() { ... var func_b = function() { ... //from here, how can I access the contents of var_a? //(without using A.var_a since there are multiple instances of A) //this.a doesn't work, I realize I need to traverse up, //but how can I do that? } } } Link to comment https://forums.phpfreaks.com/topic/41799-solved-multiple-instances-and-member-variables/ Share on other sites More sharing options...
fenway Posted March 8, 2007 Share Posted March 8, 2007 You can't actually access other hash keys during declaration. Link to comment https://forums.phpfreaks.com/topic/41799-solved-multiple-instances-and-member-variables/#findComment-202719 Share on other sites More sharing options...
neoform Posted March 8, 2007 Author Share Posted March 8, 2007 So what sort of work around could i use them? The child function is an action for an <a> tag, and it needs to be able to interact with the parent object.. right now it does that if I set it to access A, but when it does that, multiple instances all share the same object.. Link to comment https://forums.phpfreaks.com/topic/41799-solved-multiple-instances-and-member-variables/#findComment-202737 Share on other sites More sharing options...
fenway Posted March 8, 2007 Share Posted March 8, 2007 You'd have to assign each hash key individually, and then refer to the "previously declared" ones. Link to comment https://forums.phpfreaks.com/topic/41799-solved-multiple-instances-and-member-variables/#findComment-202998 Share on other sites More sharing options...
neoform Posted March 9, 2007 Author Share Posted March 9, 2007 actually, I post this on another site and someone pointed me to this: http://www.digital-web.com/articles/scope_in_javascript/ and i found this delightful script: Function.prototype.bind = function(obj) { var method = this, temp = function() { return method.apply(obj, arguments); }; return temp; } by binding the child function to the parent, you can then use the member functions of the parent. var A = new Object(); A = { var_a : 'hello', func_a : function() { ... var func_b = this.func_bb.bind(this); }, func_bb : function() { ... alert(this.var_a); } } Link to comment https://forums.phpfreaks.com/topic/41799-solved-multiple-instances-and-member-variables/#findComment-203438 Share on other sites More sharing options...
fenway Posted March 9, 2007 Share Posted March 9, 2007 Yeah, that's very sneaky, using closures like that. Link to comment https://forums.phpfreaks.com/topic/41799-solved-multiple-instances-and-member-variables/#findComment-203526 Share on other sites More sharing options...
neoform Posted March 9, 2007 Author Share Posted March 9, 2007 Link to comment https://forums.phpfreaks.com/topic/41799-solved-multiple-instances-and-member-variables/#findComment-203845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.