Chris W. Parker wrote:
On Thursday, June 21, 2007 9:48 AM Klaus Hartl said:

$(document).ready(function() {
 $("[EMAIL PROTECTED]'section']").click(function(){
  $(this)[this.checked ? 'addClass' : 'removeClass']('checked'); });
});

What kind of syntax is that third line? (I don't mean the ternary
operator.)



myObject.foo is the same as myObject['foo'] in JavaScript. It doesn't matter which datatype the property is of.

So if foo were a function you could call it in two ways:

myObject.foo();

or

myObject['foo']();


This is very handy in the case you need to decide at runtime which function too call (thus the ternary operator) and helps to avoid redundant code.

Compare to this which does the same:

if (this.checked) {
    $(this).addClass('checked');
} else {
    $(this).removeClass('checked');
}



--Klaus



Reply via email to