SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,629
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Is There a Graceful Way to Pass this to an Anonomous Function

    Hey guys, I've recently been working on getting up to speed with javascript. I am really well-versed with ASP.NET and C#, so I suspect I probably do alot of things funny from a scripting perspective. Anyhow, one thing I have often thought was really hokey was my workaround to access a reference to the current object (this) inside a jquery callback. My usual trick goes something like the last couple lines of the method below:

    Code JavaScript:
    //starts the image rotation. This must be called first.
    ImageRotator.prototype.startRotation = function(target, images, interval)
    {
        //can't restart rotation
        if (!this.rotationStarted)
        {
            var nextImg=this.nextImage;   
            //set our target jquery object
            this.target=target;
            //sort the images randomly.
            this.images = this.shuffle(images);
            //set killswitch
            this.rotationStarted=true;
            //show the first image
            this.showNextImage();
            //fire off rotation
            var ir = this;
            //start image rotation
            setInterval(function() { ir.showNextImage(); }, interval);
        }  
    }

    [feel free to let me know if you need more code, I can post the full object if need be but I thought this would be an adequate example]

    So, scripting gurus, please let me know if there is a better, more elegant way to handle these cases, especially when using jQuery.

    Thanks in advance.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can create a general purpose utility function for that case:
    Code:
    function bind(method, obj) {
      return function() {
        return obj[method]();
      }
    }
    Usage:
    Code:
    setInterval(bind("showNextImage", ir), interval);
    Or even:
    Code:
    function bind(method, obj) {
      if (typeof(method) == "function") {
        return function() {
          return method.apply(obj);
        }
      }
      return function() {
        return obj[method]();
      }
    }
    Usage:
    Code:
    setInterval(bind(ir.showNextImage, ir), interval);
    Most javascript frameworks have something similar (This particular one, is a simplified version of MochiKit's bind), and I would expect jQuery to be no different in this respect. I haven't used it enough to know for sure though.


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
  •