* Jason Hummel <[EMAIL PROTECTED]> [2005-10-27 12:09]:
> Thanks for the feedback, the bind(this) worked like a charm, I was
> beating my head against my monitor trying to come up with a solution.
> 
> I'm confused, though, as to why javascript loses the reference to
> "this" without that bind method can somebody explain?

function ThingSayFoo() { alert('foo! ' + this + this.location); }

function Thing() { this.location = 'thing'; return this; }
Thing.prototype = { sayFoo: ThingSayFoo }

function Amajig() { this.location = '-a-ma-jig'; return this; }
Amajig.prototype = { sayFoo: ThingSayFoo }

// Called directly, this is the brwoser window, closures at work.
ThingSayFoo()

// Thing is this.
new Thing().sayFoo();

// Amajig is this.
new Amajig().sayFoo();

Didn't see the orginal code, but a refernece to this is not lost. It
is added. And it is added based on how the function is called.

Bind keeps the desired target object in a closure, and calls the
member function "though" the object. Assigning the method directly
means it is called directly.

Cheers.

--
Alan Gutierrez - [EMAIL PROTECTED] - http://engrm.com/blogometer/
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to