Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Get Instance Name from within Class? 1

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
I would like to pass the reference of the instance of class A from within itself to class B, so that I can perform a callback from class B, once it has completed its processing.

How is this possible?

My current work-around, which I am trying to get away from, is hard-coding the class A instance that I declared outside of either classes, inside of class B. Something like this:
Code:
var myClass = new classA();

function classB()
{
    function do_this()
    {
//      [...]
        myClass.do_that();
    }
}
What I would like to get to is something like this:
Code:
var myClass = new classA();
myClass.run_me_first();

function classA()
{
    function run_me_first()
    {
//      [...]
        var mememe = new classB();
        classB.do_this([reference to this class here]);
    }

    function do_that()
    {
//      [...]
    }
}

function classB()
{
    function do_this(o_class)
    {
//      [...]
        o_class.do_that();
    }
}

I'm trying to get my head around this. Thanks!

Take Care,
Mike
 
You were pretty close with the code you had. I just changed your private functions into public methods and passed in the classA instance using the "this" keyword:
Code:
<script>
var myClass = new classA();
myClass.run_me_first();

function classA()
{
    this.run_me_first = function()
    {
//      [...]
        var mememe = new classB();
        mememe.do_this([red][b]this[/b][/red]);
    }

    this.do_that = function()
    {
	alert('classA.do_that() called!');
//      [...]
    }
}

function classB()
{
    this.do_this = function(o_class)
    {
//      [...]
        o_class.do_that();
    }
}
</script>
Just remember that the "this" keyword refers to the method's owner. So [tt]myClass.run_me_first()[/tt] will work, but if you use [tt]window.onload = myClass.run_me_first;[/tt] you are making the contents of the run_me_first method an anonymous function of the window object which will cause the "this" keyword to refer to the window object.

Adam
 
THANK YOU SO MUCH, Adam!!!

I was wondering what was going on wrong. That's exactly what was happening: I was executing the window.onload event.

I was trying using this and public methods (like you pointed out), but it always passed the window object, which baffled me.

So what you are saying is that I cannot run it onload using events. I have to manually call the class and instanciate it in a variable. This gets me to thinking if this might not work:
Code:
<script>
var myClass = new classA();

function classA()
{
    this.run_me_first = function()
    {
//      [...]
        var mememe = new classB();
        mememe.do_this(this);
    }

    this.do_that = function()
    {
    alert('classA.do_that() called!');
//      [...]
    }
}

function classB()
{
    this.do_this = function(o_class)
    {
//      [...]
        o_class.do_that();
    }
}

function load_me()
{
    myClass.run_me_first();
}

window.onload = function() {load_me();)
</script>

Let me know what you think. In the meantime I will experiment with these ideas.

Woot! **elated**
Thanks again! :)

Mike

Take Care,
Mike
 
I made an observation while looking at my code. I currently declare the class and call the member function of the instantiated class without using the window.onload() (commented out for testing), and yet, the parent - or the this object within the class still refers to the window object.
What am I doing wrong? See the example code, which correctly refers to the object when using this:
Code:
function test_class()
{
	this.run_me = run_me;
	
	function run_me()
	{
		alert(this);
	}
}

var me = new test_class();
me.run_me();

I'm hesitant to post my complete code here, could I email you? Please contact me at "bronner DOT mike AT NOTSPAM gmail DOT com" if it's OK.

I'm looking at my code and can't see why the this object wouldn't act in the same manner and loose its context to the object?!

Thanks again!
Mike

Take Care,
Mike
 
I've narrowed down the problem. My above example works fine, but when I call another member function from within the class, it looses scope and this becomes the window object. Consider the following:
Code:
function test_class()
{
	this.run_me = run_me;
	
	function run_me()
	{
		run_second();
	}
	
	function run_second()
	{
		alert(this);
	}
}

var test = new test_class();
test.run_me();

I'm probably calling the second member function incorrectly. Any suggestions?

Thanks again!
Mike

Take Care,
Mike
 
Doing that causes the script to wonder who its maker is, and it replies that it cannot find this.run_second().

Take Care,
Mike
 
This is because you do not have a property called run_second. So what BillyRayPreachersSon said is correct. You just have to define the property. This is one of the reasons why I use the syntax that I used above - so I don't have to worry about forgetting this step.
Code:
<script>
function test_class()
{
    this.run_me = run_me;
    [red]this.run_second = run_second;[/red]
    
    function run_me()
    {
        [red]this.[/red]run_second();
    }
    
    function run_second()
    {
        alert(this);
    }
}

var test = new test_class();
test.run_me();
</script>

Adam
 
I see what your saying. However, I understand the difference between defining class properties for member functions as the difference between private and public methods in conventional OOP programming. Is this in fact NOT the case?

I will try your suggestions, and keep you posted.

Thanks again! :)

Take Care,
Mike
 
I don't quite understand your last question, but I think I understand what you are looking for. You want to expose run_me as a public method and hide run_second as a private method that can access the current instance of the object, right? One way would be to assign the current instance of the object to a private variable in the constructor. Then you can use the private variable and you don't have to worry about what 'this' refers to.
Code:
<script>
function test_class()
{
    [red]var instance = this;[/red]
    this.run_me = run_me;
    
    function run_me()
    {
        run_second();
    }
    
    function run_second()
    {
        alert([red]instance[/red]);
    }
}

var test = new test_class();
test.run_me();
</script>

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top