#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20

    Apply() when context is unknown.


    Hi,
    The following code shows a method that takes a list of functions and a list of arguments and calls each of those functions with the arguments listed in the array.
    Code:
    SomeObj.prototype.makeCalls = function( callList, arguments ){
    	for( var i=0; i<callList.length; i++ ){
    		callList[i].apply( this, arguments );
    	}
    
    	return true;
    }
    The problem is that apply() resets the context 'this' to the current context, which is 'SomeObj', right?

    However, the functions in the callList should not lose their original context, but what context they have is unknown. Is there a way (besides using eval() to not lose the context but still call the functions with all the arguments in the array?

    Thanks!
    Stefan1
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,932
    Rep Power
    4033
    The problem is that apply() resets the context 'this' to the current context, which is 'SomeObj', right?
    apply would set the context (ie, the this variable) of each of those function calls to whatever is passed in as it's first parameter. In this case, you are using the current context of the original makeCalls function. Within makeCalls, this will be whatever object the function was called on. Eg, if you have:
    Code:
    var o1 = new SomeObject();
    var o2 = new SomeObject();
    
    o1.makeCalls([a,b,c], [d,e,f]);  //this would be o1
    o2.makeCalls([a], [b]); //this would be o2
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20
    Ah yeah you're right.

    But what if don't want to use the context of whatever makeCalls was called on. I mean in Jquery you have this proxy function to make a method always have a particular context. Does apply affect the working of this? There is also the bind "trick" below. What if i use apply() on a function that has been bound on a particular object?

    Code:
    Function.prototype.bind = function(scope) {
      var _function = this;
      
      return function() {
        return _function.apply(scope, arguments);
      }
    }
    I tend to get confused when i'm having troubles with contexts in javascript.

    Grtz Stefan
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,932
    Rep Power
    4033
    Originally Posted by Stefan1
    What if i use apply() on a function that has been bound on a particular object?
    It won't change the scope of the function that was bound. The process of binding returns a new function, which when called will execute the original function using the context that was provided at the time of the binding. Whatever context is applied at the time the function is called is essentially ignored.

    Comments on this post

    • Stefan1 agrees
    Last edited by kicken; September 18th, 2013 at 05:48 PM.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20
    Ah ok, thanks alot!

IMN logo majestic logo threadwatch logo seochat tools logo