SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Object cross-references

    Hi!

    Guess whos back with more reference problems. I have created two objects, ObjectA and ObjectB. I would like to have ObjectB use a function beloning to ObjectA as a callback function when the execute() method has finished.

    Code:
    function ObjectA(value)
    {
    	this.internal = value;
    }
    
    ObjectA.prototype.callback = function()
    {
    	alert(this.internal);
    }
    
    function ObjectB()
    {
    	this.callback = null;
    }
    
    ObjectB.prototype.execute = function()
    {
    	if( this.callback != null )
    	    this.callback();
    }
    
    function Init()
    {
    	var a = new ObjectA(10);
    	var b = new ObjectB();
    	b.callback = a.callback;
    	b.execute();
    }
    when I try to run the code, the alert shows "undefined". So I thought this was the same problem I had in my other post (for those of you who read it) and I changed the callback-assigning to

    Code:
    b.callback = function(){a.callback};
    but when this is executed..no alert is displayed.. I dont think the callback in ObjectA is called at all (or rather I'm sure it isn't). Any ideas on what the solution would be ?

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    b.callback = function(){a.callback};
    That function on the right is equivalent to this:
    Code:
    var num = 10;
    function(){num};
    It doesn't do anything. 'a.callback' is the name of a property of an object, and you don't do anything with it.

    This line:
    Code:
    b.callback = a.callback;
    overwrites b's callback property with a.callback which is equal to:
    Code:
    function()
    {
    	alert(this.internal);
    }
    So, you get:
    Code:
    b.callback = function()
    {
    	alert(this.internal);
    }
    Then, you do this:
    Code:
    b.execute();
    where execute looks like this:
    Code:
    function()
    {
    	if( this.callback != null )
    	    this.callback();
    }
    Since b is calling execute(), then inside the execute() function 'this' equals b. Substituting b for 'this' in execute() gives you:
    Code:
    function()
    {
    	if( b.callback != null )
    	    b.callback();
    }
    Since b is calling the callback() function, then inside callback() 'this' equals b. Substituting b for 'this' in callback() gives you:
    Code:
    function()
    {
    	alert(b.internal);
    }
    Since b doesn't have a property called internal, undefined is displayed.

  3. #3
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud,

    Gotcha! But, is there any way to use the cross-object reference for the callback, ie. so that execute that call the callback method of ObjectA and have it execute in the context of ObjectA (where this = a) ??

  4. #4
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any ideas ?

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope.

  6. #6
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if this helps.

    It works, but holds the potential of overwriting methods in a problematic way as is.

    Code:
    function ObjectA(value)
    {
    	this.internal = value;
    }
    
    ObjectA.prototype.callback = function()
    {
    	alert(this.internal);
    }
    
    function ObjectB(objs)
    {
    	this.objs = objs || new Array();
    	this.callback = null;
    }
    ObjectB.prototype.addObj = function(obj)
    {
    	this.objs.push(obj);
    }
    
    ObjectB.prototype.execute = function()
    {
    	if( this.callback != null )
    	{
    		for(obj in this.objs)
    		{
    			this.objs[obj].constructor.prototype.callback = this.callback;
    			this.objs[obj].callback();
    		}
    	}
    }
    
    function Init()
    {
    var a = new ObjectA(10);
    var b = new ObjectA(20);
    
    var c = new ObjectB([a,b]);
    
    c.callback = a.callback;
    
    c.execute();
    }
    window.onload = Init;

  7. #7
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joebert
    It works, but holds the potential of overwriting methods in a problematic way as is.
    Then again, we could always bust out our MIB issued memory zapper thingy & have the objects forget they ever learned anything.
    Code:
    function ObjectB(objs)
    {
    	this.objs = objs || new Array();
    	this.callbackReference = '_' + Date.parse(new Date());
    	this.callback = null;
    }
    
    ObjectB.prototype.execute = function()
    {
    	if( this.callback != null )
    	{
    		for(obj in this.objs)
    		{
    			this.objs[obj].constructor.prototype[this.callbackReference] = this.callback;
    			this.objs[obj][this.callbackReference]();
    			this.objs[obj].constructor.prototype[this.callbackReference] = null;
    		}
    	}
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •