Skip to content

Instantly share code, notes, and snippets.

@kanonji
Created April 21, 2011 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanonji/934093 to your computer and use it in GitHub Desktop.
Save kanonji/934093 to your computer and use it in GitHub Desktop.
Confirmation code to see in what 'this' is.
var Try = function(){
this.bar = 'bar';
$(window).bind( "load", { self: this }, this.loaded );
};
Try.prototype = {
loaded: function( e ){
console.info('------loaded------');
console.debug(this);
this.foo = 'loaded';
$('body').append( $('<ul>').attr('id', 'foo').append($('<li>')).append($('<li>')) );
console.info('---try1---');
e.data.self.try1();
console.info('---try2---');
e.data.self.try2();
$('ul#foo li').each(function(index){
console.debug(index);
console.debug(this);
console.debug(this.foo);
console.debug(this.bar);
});
e.data.self.end();
},
init: function(){
console.info('------init------');
console.debug(this);
this.foo = 'init';
$('body').append( $('<ul>').attr('id', 'foo').append($('<li>')).append($('<li>')) );
console.info('---try1---');
this.try1();
console.info('---try2---');
this.try2();
this.end();
},
try1: function(){
console.debug(this);
console.debug(this.foo);
console.debug(this.bar);
(function(){
console.debug(this);
console.debug(this.foo);
console.debug(this.bar);
})();
},
try2: function(){
console.debug(this);
console.debug(this.foo);
console.debug(this.bar);
$('ul#foo li').each(function(index){
console.debug(index);
console.debug(this);
console.debug(this.foo);
console.debug(this.bar);
});
},
end: function(){
$('body').remove();
}
};
var t = new Try();
t.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment