On Jan 16, 2011, at 5:32 AM, satyr wrote:
> Speaking of `new`, I prefer former because of its presence:
>
> o = new (function(){
> try { f() } catch(_){ return A }
> return B
> }()) // can't be `})()`
>
> Here the intention is not to invoke the wrapper, but to make an
> instance off of the return value.
>
I would avoid this pattern since it can be confusing and it's harder to
scale... I would probably do something like:
var myFactory = (function(){
function create(){
// do almost the same thing you've done but return a "new" object
// instead of a reference to the constructor
}
// --- API --- //
return {
create : create
}
}());
o = myFactory.create();
that way it is easier to reuse the method to create other objects of the same
kind and also you split the implementation from where you are using it.. it's
more code but also more organized.
for me one of the biggest "problems" of JavaScript is that there is 1000 ways
of doing the same thing... that's why IDEs can't do automatic error check or
autocomplete properly and also the reason why books like Stoyan's JavaScript
Patterns are so important..
interesting read about design patterns:
http://blog.plover.com/prog/design-patterns.html
cheers.
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]