http://ejohn.org/blog/building-a-javascript-library/
In the above link John Resig Suggests calling and returning new foo
in a constructor if the caller originally forgot to.
that makes some sense to me, but then I get a strict error because my constructor does not 'always' return a value. Upon gaining a little understanding of constructors in javascript, i stopped returning this
because new
automatically does that.
My question is, should I ...
returning this
is pointless because if the caller forgot to add new
then this
will be the document, not an instance of foo
. What i usually do is start a constructor with if(! (this instanceof foo)) return new foo();
or something to that effect
Edit: after a more careful read, if you want to avoid strict errors and you are doing this already, yes, return this
at the end is usually a good idea
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With