What is the compliance status of assigning to exports?

36 views
Skip to first unread message

jon.seymour

unread,
Apr 18, 2011, 8:32:59 AM4/18/11
to CommonJS
I am trying to work out where constructions of the form are possible
in Module 1.1.1?

var bar=require("foo")();

This, of course, requires that a module can assign to the exports free
variable. Is that allowed by the specification?

The specification doesn't explicitly exclude such an assignment, but I
am unclear as to whether it actually permits it.

Are there any plans to change the specification to make the answer to
this question less subject to the reader's interpretation?

jon.

Wes Garland

unread,
Apr 18, 2011, 10:29:00 AM4/18/11
to comm...@googlegroups.com
Assigning to exports is not part of the specification, although some implementations support it.

It is unclear if this will be expressly permitted in the future or not.  If you are interested in writing portable code today, do not use this feature.

Wes


--
You received this message because you are subscribed to the Google Groups "CommonJS" group.
To post to this group, send email to comm...@googlegroups.com.
To unsubscribe from this group, send email to commonjs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/commonjs?hl=en.




--
Wesley W. Garland
Director, Product Development
PageMail, Inc.
+1 613 542 2787 x 102

jon.seymour

unread,
Apr 19, 2011, 2:41:25 AM4/19/11
to CommonJS
On Apr 19, 12:29 am, Wes Garland <w...@page.ca> wrote:
> It is unclear if this will be expressly permitted in the future or not.  If
> you are interested in writing portable code today, do not use this feature.

Thanks Wes. Even if a decision has yet to be made, I think it would be
good if a revision to the specification could provide the advice you
have given here. Specifications, after all, are meant to be the source
of truth of such wisdom.

For now, I will replace any use of the idiom:

var bar=require("foo")();

with:

var bar=require("foo").use();

jon.

Wes Garland

unread,
Apr 19, 2011, 9:12:26 AM4/19/11
to comm...@googlegroups.com
On 19 April 2011 02:41, jon.seymour <jon.s...@gmail.com> wrote:
On Apr 19, 12:29 am, Wes Garland <w...@page.ca> wrote:
 
Thanks Wes. Even if a decision has yet to be made, I think it would be
good if a revision to the specification could provide the advice you
have given here. Specifications, after all, are meant to be the source
of truth of such wisdom.

Truth, for sure. Wisdom, debatable. :)

The next-spec schedule is very much up in the air, but clarifying assignable exports *is* on the agenda.

For now, I will replace any use of the idiom:

   var bar=require("foo")();

with:

   var bar=require("foo").use();

FWIW, I personally prefer

var myChair = new require("bar").Chair();

but that's a personal style thing.
 
If you are writing server-side code on a Mozilla-based platform, you can do this, too, which I think is pretty awesome:

var { Chair, PoolTable, Glass } = require("bar");
var myChair = new Chair();

That first line shows deconstructing assignment, where the three properties from the exports are matched up by name.  That is a Mozilla-only extension, though, but it will probably show up in ECMAScript in a few years.

Wes

Reply all
Reply to author
Forward
0 new messages