Jump to content

[SOLVED] Multiple Instances and member variables..


neoform

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.