JavaScript

Binding "this" in JavaScript: I finally get it!

var obj = {
  name: "Richard",
  callback: function(event) {
    print("event = " + event);
    print("name  = " + this.name);
  }
};
var element = {
  name: "Element",
  callback: null,
  invoke: function() {
    print("invoke");
    if (this.callback != null) {
      this.callback("hello");
    }
  }
};
function bind(context, func) {
  return function() {
    return func.apply(context, arguments);
  }
}
element.callback = bind(obj, obj.callback);
element.invoke();

My first Google Code project: JSHumanizer

I'm sure there's stuff out there to do this already, but I want to administer a Google Code project dammit! And here it is...

This is used throughout my personal web sites and thought I'd share it with the world!

Syndicate content