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?
	}
}
}

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..

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);
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.