Question: Misuse of variables bound to exports object?

30 views
Skip to first unread message

akira

unread,
Apr 28, 2012, 12:54:36 AM4/28/12
to nodejs
I am sorry if this is not node related, I just thought this might be a
good place to ask since a lot of module developers are on this list.

Using this simple example, I would like to inherit a form

exports.EmailForm = forms.Form.extend({
email_address : forms.EmailField({required: true})
})

exports.ExtendedEmailForm = exports.EmailForm.extend({ // <- Extending
EmailForm
subject : forms.CharField({maxLength: 100})
, message : forms.CharField()
, sender : forms.EmailField({required: true})
})


Is this a misuse? It works, but normally one would do something like
this:

var EmailForm = forms.Form.extend({
email_address : forms.EmailField({required: true})
})


But I would like to export the simple EmailForm class for usage in
other modules too. Thanks

Dominic Tarr

unread,
Apr 28, 2012, 7:17:47 AM4/28/12
to nod...@googlegroups.com
it's a bit ugly i guess, but there is nothing wrong with it.

that it's named exports tells you quite a lot actually.
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Oliver Leics

unread,
Apr 28, 2012, 7:18:17 AM4/28/12
to nod...@googlegroups.com
Readability taken aside, I can't see how this is could be a 'misuse'
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en



--
Oliver Leics @ G+
https://plus.google.com/112912441146721682527

akira

unread,
Apr 28, 2012, 8:03:13 AM4/28/12
to nodejs
Thanks for the replies

C. Mundi

unread,
Apr 28, 2012, 11:14:42 AM4/28/12
to nod...@googlegroups.com

I used to do the same thing only worse in my code, namely chaining extensions to exports...sometimes four or more deep.  This is a habit I brought from statically typed OOP. 

I don't do it anymore in JS, because it made refactoring a nightmare.  The "problem" is...unless you have really good unit test coverage...the chain can easily be broken and you don't find out until way up the callstack.   All it takes is the order changing or something inserted in between.   That's the price of prototypes in a dynamic language...no compiler can tell if you really mean what you wrote. 

So now what I do is at most one layer of chaining...a common base and then every extension is self contained even though that means a little more typing.  It's working for me.  So I think what you have is fine, as long as you don't go to a third level.

Reply all
Reply to author
Forward
0 new messages