Playing with 'this'

Experiments with 'this'

by Andy Bulka

HTML

<h1>Playing with 'this'</h1>
<ul>
    <li><a href="http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/">Understanding JavaScript’s this keyword</a></li>
    <li><a href="http://bonsaiden.github.com/JavaScript-Garden/">JavaScript-Garden</a></li>
    <li><a href="http://devign.me/javascript-this-keyword/">javascript-this-keyword (good)</a></li>
    
</ul>

JavaScript

console.log('in global, this is simply', this);

var f1 = function() { console.info('inside regular function, this=', this); }
f1();

f2 = new function() { console.info('inside regular function via NEW, this=', this); }
//f2();


var o={}; // declaring new object
o.name="moon";
o.method=function () {
    console.info('inside object, calling a method, this=', this, this.name);
};
o.method();

// Carl's version
var bob = function() {
 var self = arguments.callee;
 console.info('inside bob', this, self, self.aa);
}
bob.aa = 16;
bob();