ES Style Question

# Rick Waldron (14 years ago)

I was wondering if a canonical guide for ECMAScript style and conventions exists - specifically I'm curious to find out what the historic precedence, rules and reasoning behind the capitalization of constructors and built-in objects, ie. Array or Math. Even more specifically, there exists a fairly common convention that first letter capitalization is generally reserved for constructors that expect to be paired with "new"... obviously this is not a hard rule, but I'm curious if any documentation or articles exist regarding the subject.

# Mark S. Miller (14 years ago)

I like javascript.crockford.com/code.html , though I disagree with leaving a space between "function" and "(" for anonymous functions.

www.jslint.com

# Dmitry Soshnikov (14 years ago)

Also, style guides with 2 spaces indention are also good and wide-spread (other things are the same as described in the link which Mark gave

# Tom Schuster (14 years ago)

var a = 10 , b = 20 , c = 30;

Ugh the first time i saw this, i wondered who came up with this.

The Google style guide also isn't too bad google-styleguide.googlecode.com/svn/trunk/javascriptguide.xmlcript.crockford.com/code.html , though I disagree with

/wrong button

# Rick Waldron (14 years ago)

Thanks for all the input. I'm actually aware of and have studied quite thoroughly both of the resources that were provided - I'm kind of a "stickler" for style guides.

I was actually very specifically looking for an answer regarding First letter casing for non-constructor built-in objects, ie. Math and Proxy. I think the legacy Java style guide influence addresses the question as best as I can hope for.

# Mikeal Rogers (14 years ago)

I've used a few different javascript style guides over the years and the only thing I can remember being consistent across all of them is that only constructors get First letter casing.

I have to admit that the few times I've seen people using First letter casing for non-constructors I've immediately decided that their code is no good, which is probably a little reactionary on my part.

Crockford puts it well:

"Constructor functions which must be used with the new prefix should start with a capital letter. JavaScript issues neither a compile-time warning nor a run-time warning if a required new is omitted. Bad things can happen if new is not used, so the capitalization convention is the only defense we have."

-Mikeal

PS. I've now more or less settled on Isaac's style guide which is used by npm isaacs/npm/blob/master/doc/coding-style.md although I do use semicolons on new lines after large var declarations because I find that newer developers think the statement ends ambiguously.

# Wes Garland (14 years ago)

On 9 September 2011 16:06, Rick Waldron <waldron.rick at gmail.com> wrote:

I was actually very specifically looking for an answer regarding First letter casing for non-constructor built-in objects, ie. Math and Proxy. I think the legacy Java style guide influence addresses the question as best as I can hope for.

These did come to us via Java (well, not Proxy), but they are hardly a matter of style; they are a matter of specification.

Generally we name constructor functions with CapitalLeadingCamelCase and everything else with smallLeadingCamelCase. I think I picked this up from JavaScript: The Definitive Guide, 2nd Edition, by David Flanagan, about 4,000 years ago.

As for other matters of style, I am a big proponent of brace-on-a-line in C-syntax languages, however this has proven to be a problem in practice with JavaScript due to the interaction between automatic semicolin insertion and object literal syntax in return statements:

return { Hello: "world" };

is not the same as

return { Hello: "world" }

# Rick Waldron (14 years ago)

Mikeal,

So, the Math object is "no good"?

Rick

-- Sent from my Palm Pre On Sep 9, 2011 4:27 PM, Mikeal Rogers <mikeal.rogers at gmail.com> wrote:

I've used a few different javascript style guides over the years and the only thing I can remember being consistent across all of them is that only constructors get First letter casing.  I have to admit that the few times I've seen people using First letter casing for non-constructors I've immediately decided that their code is no good, which is probably a little reactionary on my part. Crockford puts it well: "Constructor functions which must be used with the new prefix should start with a capital letter. JavaScript issues neither a compile-time warning nor a run-time warning if a required new is omitted. Bad things can happen if new is not used, so the capitalization convention is the only defense we have." -Mikeal PS. I've now more or less settled on Isaac's style guide which is used by npm isaacs/npm/blob/master/doc/coding-style.md although I do use semicolons on new lines after large var declarations because I find that newer developers think the statement ends ambiguously. On Sep 9, 2011, at September 9, 20111:06 PM, Rick Waldron wrote:Thanks for all the input. I'm actually aware of and have studied quite thoroughly both of the resources that were provided - I'm kind of a "stickler" for style guides. I was actually very specifically looking for an answer regarding First letter casing for non-constructor built-in objects, ie. Math and Proxy. I think the legacy Java style guide influence addresses the question as best as I can hope for.

Thanks Rick

On Fri, Sep 9, 2011 at 3:11 PM, Tom Schuster <tom at schuster.me> wrote:

var a = 10

, b = 20

, c = 30;

Ugh the first time i saw this, i wondered who came up with this.

The Google style guide also isn't too bad

google-styleguide.googlecode.com/svn/trunk/javascriptguide.xmlcript.crockford.com/code.html

, though I disagree with

/wrong button

# Rick Waldron (14 years ago)

Wes,

I was referring to the very first reply to this thread.

Thanks again

Rick

-- Sent from my Palm Pre On Sep 9, 2011 4:48 PM, Wes Garland <wes at page.ca> wrote:

On 9 September 2011 16:06, Rick Waldron <waldron.rick at gmail.com> wrote:

I was actually very specifically looking for an answer regarding First letter casing for non-constructor built-in objects, ie. Math and Proxy. I think the legacy Java style guide influence addresses the question as best as I can hope for.

These  did come to us via Java (well, not Proxy), but they are hardly a matter of style; they are a matter of specification.

Generally we name constructor functions with CapitalLeadingCamelCase and everything else with smallLeadingCamelCase.  I think I picked this up from JavaScript: The Definitive Guide, 2nd Edition, by David Flanagan, about 4,000 years ago.

As for other matters of style, I am a big proponent of brace-on-a-line in C-syntax languages, however this has proven to be a problem in practice with JavaScript due to the interaction between automatic semicolin insertion and object literal syntax in return statements:

return{   Hello: "world" };

is not the same as

return {  Hello: "world" }

# Brendan Eich (14 years ago)

On Sep 9, 2011, at 1:48 PM, Wes Garland wrote:

On 9 September 2011 16:06, Rick Waldron <waldron.rick at gmail.com> wrote: I was actually very specifically looking for an answer regarding First letter casing for non-constructor built-in objects, ie. Math and Proxy. I think the legacy Java style guide influence addresses the question as best as I can hope for.

These did come to us via Java (well, not Proxy), but they are hardly a matter of style; they are a matter of specification.

They're a matter of style, ultimately (in Java).

Generally we name constructor functions with CapitalLeadingCamelCase and everything else with smallLeadingCamelCase.

Math and other "leaf package names" in Netscape's implementation are StudlyCaps, same as in Java. We'll see how people name modules (the importer binds the name, so this is all up to users' style preferences in ES6).