SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do you make use of the .call method of functions?

    Without .call:
    Code:
    function changeBackground(obj, new_color) {
      document.getElementById(obj).style.backgroundColor = new_color;
    }
    
    changeBackground('mydiv', 'red');
    With .call:
    Code:
    function changeBackground(new_color) {
      this.style.backgroundColor = new_color;
    }
    
    changeBackground.call(document.getElementById('mydiv'), 'red');
    You don't tend to see it used very often, I just wondered if people are not aware of it, or they are but choose not to use it to pass object references between functions?

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    apply/call is very useful indeed!

    apply/call was unsupported in jscript until v5.5 so if you had to support IE 4 or 5.0 you could not use it.

    Also I think only in the last couple of years have we seen extensive use of try/catch, which was first supported in jscript v5.

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use call for inheritance:
    Code:
    function Animal() {
    
    }
    
    function Dog() {
        Animal.call(this);
    }
    
    Dog.prototype = new Animal();

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster View Post
    apply/call was unsupported in jscript until v5.5 so if you had to support IE 4 or 5.0 you could not use it.
    Yes, but call can be simulated for IE 5 and older, check out this blog comment.

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    call is a good way to loop through a collection of html elements and
    perform the same operations on each-
    and if you have several objects that can use the same method,
    it saves having to define it for each.

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    call is a good way to loop through a collection of html elements and
    perform the same operations on each-
    and if you have several objects that can use the same method,
    it saves having to define it for each.
    Sounds interesting, never used it like this, can you please provide an example?

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a method of an object that creates associative arrays of
    elements. The sort method of the object gets an array to loop through,
    so it could also work on an Array of elements.

    Code:
    	callAll: function(fun,args){
    		var A=this.sort();
    		var L= A.length;
    		while(L) fun.call(A[--L],args);
    		if(arguments[2]===true)return A;
    	}
    Last edited by mrhoo; Dec 18, 2006 at 16:50.


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
  •