[thelist] JavaScript Functions and correctly returning values

Mark Howells webdev at mountain.ch
Fri Aug 16 06:46:01 CDT 2002


>> function Collection() {
>>   this.length = 0;
>>   this.add = add;
>>   return this;
>> }
>>
>> This is a constructor function. I've never seen "return this" at the
>> end of a constructor. It's certainly not required, but does this go
>> along with my first question? I'm assuming that saying return this
>> will in fact return the value of all the objects properties.

I use similar functionality in all of my DHTML projects -- see the example
code at <http://www.mark.ac/help/jsObjects.html>. Using "return this" at the
end if the function simply ensures that the new Javascript object receives
all the properties defined by the function, and it's completist -- I've
tested in current browsers, and the function seems to work with or without
the return.

Regards
Mark Howells
<http://www.mark.ac/evl/>




More information about the thelist mailing list