> Let's look at the method checkAll in Contact object:
> var Contact = {
>     initialize: function(json,id_contact){
> $('check-all').observe('click',this.checkAll.bindAsEventListener(this));
>     },
>     checkAll: function(e) {
>         e.stop();
>         this.contactChecked = [];
>         $$('#contacts-ul .contact-checkbox').each(function(c){
>                 c.checked = true;
>                 Contact.contactChecked.push(parseInt(c.value));
> Here 'this' is a window object so I could use bind(this) and then use
> this.contactChecked,
> but I can skip bind and use Contact.contactChecked which I often do.

The behaviour depends on whether you call the constructor with 'new'
or not (another of the features that Crockford describes as 'very
If you call it as 'new Contact', than inside the 'initialize', 'this'
refers to the new Contact object you are creating, and checkAll will
be bound so that inside it 'this' also refers to that Contact object.

If you call the constructor without 'new', then 'this' refers to the
global object, and that is what will be bound as 'this' inside

So yes, for this sort of use you usually do need 'bind'. You very
rarely need 'bindAsEventListener', and I cannot remember what the
special case is that it is designed for.

You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to