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? } } } Quote Link to comment 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. Quote Link to comment 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.. Quote Link to comment 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. Quote Link to comment 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); } } Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
neoform Posted March 9, 2007 Author Share Posted March 9, 2007 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.