Flash Games
 FAQ   Search   Memberlist   Usergroups   Register  Profile   Log in to check your private messages   Log in 


self = this ... or how I learned to continue worrying but lo



 

Post new topic   Reply to topic  
   DEVPPL Forum Index -> JavaScript Forum
View previous topic :: View next topic  
Author Message
epimeth



Joined: 29 May 2008
Posts: 1

PostPosted: Thu May 29, 2008 4:33 pm    Post subject: self = this ... or how I learned to continue worrying but lo Reply with quote

so here's a *greatly simplified* snippet:
it uses the YUI event handler functions... if you don't know them then suffice it to say that:

YAHOO.util.Event.on ( el , sType , fn , obj , override )
Appends an event handler

Parameters:
el <String|HTMLElement|Array|NodeList> An id, an element reference, or a collection of ids and/or elements to assign the listener to.
sType <String> The type of event to append
fn <Function> The method the event invokes
obj <Object> An arbitrary object that will be passed as a parameter to the handler
override <Boolean|object> If true, the obj passed in becomes the execution scope of the listener. If an object, this object becomes the execution scope.

Code:

function TopLevel() {
  function MyProblematicClass() {
    // here is why it is problematic:
    var self = null;

    var myObjectHandler = function(action, args) {
      var myProblematicFunction() {
        var myDiv = document.createElement('div');

        // here is where I use 'self' because 'this' did not work
        YAHOO.util.Event.on(myDiv, 'click', function() {
          this.someFunction();
        }, null, self);

      return myDiv;
      }

      switch(action) {
        case 'myAction':
          //here I define 'self' to be 'this'
          self = this;

          // here (the same scope as above!), 'this' *does* work
          this.someOtherFunction(myProblematicFunction());
          break;
        default:
      }
    }

    return new objectThatDefines_someFunction_and_someOtherFunction({
      objectHandler: myObjectHandler
    });
  }

  var myObj = new MyProblematicClass();
  myObj.callToThingWhichFiresTheHandler();
}


the call to myObj.callToThingWhichFiresTheHandler() doesn't actually occur where I placed it, but I think this is enough to post my question.

I can only assume that my problem lies in the fact that I instead of keeping to standard closure procedure I decided to return a reference to some newly created object. However, the code *does* work, so I really don't understand what the problem is.

If you want to get into slightly deeper specifics, the objectThatDefines_someFunction_and_someOtherFunction
is actually the ycarousel component found at http://www.billwscott.com/carousel/
Reply With Quote
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    DEVPPL Forum Index -> JavaScript Forum All times are GMT + 1 Hour
Page 1 of 1

 
 
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User: Pass:
Log me on automatically each visit:

 


Powered by phpBB © 2001, 2005 phpBB Group - Modified by DEVPPL

Flash Games - Sitemap