Object property naming conventions: camelCase or under_score?


#1

Hi, it's me again.

In More Practice Making Objects we have to once again create an object. This one has a "frame material" property, and iirc is the first time we have a property that comprises >1 word.

My question is whether the choice of underscore over camelCase (which we were previously told is general convention) is convention for object properties or if it's simply arbitrary here? Just wanted to make sure.

And what happens if I use constructor notation and write `bicycle["frame material"] = "carbon fiber" instead? I tried doing this in the dog exercise with property "age age" and the console didn't complain, but I couldn't log the age age either.

Thank you,
frisby


https://www.codecademy.com/courses/spencer-sandbox/1/7?curriculum_id=506324b3a7dffd00020bf661#


var bicycle = {
    speed: 0,
    gear: 1,
    frame_material: "carbon fiber"
};


#2

@msfrisby,
== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html


Can Object() take arguments?
#3

Thank you for your help, @leonhard.wettengmx.n. I read the direct links you recommended as well as all the others listed on the homepage I thought I'd understand.

In the code convention page, I saw this on names:

Names should be formed from the 26 upper and lower case letters (A .. Z, a .. z), the 10 digits (0 .. 9), and _ underbar....

I didn't see anything about underscores vs camelCase, and both Crockford's survey page and MDN's object page seem to use one-word names* for object keys. I did see this recommendation on Google's JS Style Guide:

In general, use functionNamesLikeThis, variableNamesLikeThis, [...] methodNamesLikeThis...

Although they also do not name keys specifically (do keys count as a type of variable?) they do mention methods, which are a type of key(?), so from this I infer that camelCase is convention in keys, too...?

*I was on the lookout for this until the very end of the JS track, but alas, all other keys in the lessons were single-word keys, too. :sob:


I also didn't see anything about spaces in keys, but in the MDN link you recommended me in another answer I did see this:

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string [...] This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime).

From this I gather that my property key can have a space, but infer this is best left to dynamically determined keys, whereas ones I make in advance are best be written in camelCase and without spaces(?).

Aside: Therefore I would guess in retrospect that I couldn't log "age age" because I can't use dot notation, which iirc I tried first out of curiosity, and because I most likely used bracket notation incorrectly.

In regards to having an empty string as a key name, how would I access that property? Would I have to access it using some kind of array-like syntax?

And what if my dynamically determined keys share the same name? How can I tell one from the other?


#4

@msfrisby,
Having a space in the property-keyName
the property-keyName must be encapsulated in single/double-quotes

var myObj = {
    "a key":"ass.Value"
};
console.log( myObj["a key"] );
for (var key in myObj) {
    console.log(myObj[key]);
}

And as in the near future you will want to do some client-server-communication
you will be confronted with JSON

var myObj = {
    "a key":"ass.Value",
    key2:"ass.Value2"
};
console.log( myObj["a key"] );
console.log( myObj.key2 );
for (var key in myObj) {
    console.log(key+"--"+myObj[key]);
}

var stringified = JSON.stringify(myObj);
console.log( typeof stringified +" == "+stringified );
var unStringified = JSON.parse(stringified);
console.log( typeof unStringified );
console.log(unStringified );

//Meet JSON
// https://www.codecademy.com/courses/javascript-beginner-en-xTAfX/0/1?curriculum_id=50802b7d5225bf0200000767


#5

Thank you, @leonhard.wettengmx.n. I thought I had put "age age" in quotes, but I might not have.

How would I call a property with an empty string key, or a property with the same key as another property's?

Thank you for the link to the lesson. Funnily enough, when I went to borrow JavaScript & JQuery I was given the Sam's book alongside it. I wasn't planning to read it but am now considering it. Regarding the course, is it abandoned? I tried quickly up to the second lesson when I got stuck and saw the Q & A does not exist (I saw something similar for the "JS 1st challenges" course).

I apologize in advance if the JSON code answers my question; I've read through it but don't understand what it's doing (I thought the lesson might give me an idea but as you know I didn't get very far :joy:).


#6

@msfrisby
it i actually part of
https://www.codecademy.com/en/tracks/teachyourself
and is meant to give you a feel/taste of the subjects....


#7

Thank you, @leonhard.wettengmx.n, I guess I thought maybe it was obsolete because it had no forum. By chance, after reading an (unrelated) article about JS objects, I think I understand a bit better what the JSON code is doing ("stringifying" and "unstringifying" objects for data transfer).