Beginner Questions about Objects

So I felt like I had learned enough about loops to move on to something else. It’s not 100% but I’ve bookmarked the page in case I ever encounter some loops in the future to ask some more questions if necessary.

The following is a list of statements of what I understand about objects in Java Script. Please tell me whether I am wrong about the following assumptions:
a) There are six primitives data types and a 7th one: objects.

b) objects are containers, they store data or functions.

c) Variables can store objects.

d) after an object, we can use { curly braces } to designate an object literal.

e) an object literal is a list of name-value pairs. Here’s an example of an object literal:

You have an object literal composed of three different data types: string, number and boolean.

f) Key-value pair is the same thing as Name-value pair.

g) A Key-value is written like this: key: value. An example would be like this:
Key: 'Fuel Type': value: 'Turbo Fuel'.

h) The key is also called the identifier.

i) If I don’t have any special character in my key, I can omit using quotation marks to indicate it’s a string.

j) Keys are also properties.

k) We use the dot operator to access to access an object’s property/key.

l) Here’s an example:
spaceship is the object, and color is one of the keys of that object.

m) We can also access an object’s property by using the bracket notation. So that’s dot operator and bracket notation as two ways to access an object’s property.

n) We do this by replacing the dot operator with brackets:
Like before, the object comes first but instead of using the . operator we simply use brackets with the object’s property inside.

o) Brackets notations are necessary for properties which have spaces, numbers or special characters. We can’t use the dot operator to do that.

p) You can update the key-value of an object with a new value.

q) You can delete the value of a key-value with the delete command.

r) a property is what an object has, a method what an object does.

s) console is an object and .log is a method for example.

t) In ES6, we can omit the colon and function when assigning a value to a property.

u) In order to call an object’s method, we use the dot operator just like before but we add a set of parenthesis as the end this time around.

v) You need to use a comma when listing multiple methods:

w) An object can be nested with another object.

x) In fact, each property from an object literal needs to be separated with a comma for each key-value.

y) You would access a nested object like so:
1: object
2: property
3. nested property of property 2.

z) 'back-up' here is also an object.

This is an area where JavaScript is poorly designed.

First, the terminology is typically that objects are instances of classes. In Objected Oriented Programming (OOP) we typically define a class (there is always one and only one class definition) then declare instances of the class and those instances are typically called objects. Those terms are not absolute but they are the convention. I think however that JavaScript has features that are inconsistent with that. I am not as familiar with JavaScript as I should be and this is an area that causes me to want to avoid it. I think that in JavaScript it is possible to define and declare an object that cannot be declared again for another object. So it can be frustratingly confusing.

In most every other language there is a clear distinction between arrays and classes. In JavaScript you will eventually discover there is an overlap and that will be confusing.

And that is essentially the same thing as JSON, including your b) through j) plus more. JSON is a subset of JavaScript; JSON excludes functions.

So I take it I’m right about everything I’ve said so far then.

I read your link about JSON. What do you mean it’s a subset of JavaScript exactly? In what sense?

Cambridge Advanced Dictionary:

The Sage:

It’s part of JavaScript then. Is there something else I should be aware regarding JSON?


I’m doing an exercise on objects: click here.

I had to:
So I wrote this:

a) the reason I need curved braces in the brackets is it’s a key-value? Is that it? Could I have just written ['Thomas'] if I didn’t want to put in an object?

JSON says:

It is based on a subset of the JavaScript Programming Language

I think it is unnecessary to say more than what has already been said.

Fair enough.

Next exercise

So I have the initial code:
1: let variable declaration with the identifier spaceship followed by the assignment operator (=) and curved braces, typical of objects.
2: first key-value
3: second key-value
4: } closing of the object
6: // comment
So I need to write the new code starting at line 7.


When I check the hint I guess:

So suppose I follow the same guideline.

Attempt 1:
I tried this:
But I got an error regarding as seen in the picture above. Unexpected string or so the console says.

I thought the object parameter in this case would’ve been ['Fuel Type'] but there’s a mistake here.

I’m pretty sure greenEnergy is right. 'Fuel Type' I think is the parameter. I think the issue is that I have a string as an object parameter perhaps.

Could I get a hint?

arrow notation is being used for those functions, which hasn’t been done correctly because the parameter list must go where you highlighted the string.

Assume you are right or that I am too tired to say anything.

Days ago I suggested that you reference the relevant documentation. I did not mean the tutorial, I meant the documentation. The tutorial is not documentation.

I think one reason I do not like Arrow Functions is that people have gotten sloppy about documentation. Probably Arrow functions - JavaScript | MDN is not as clear as it could be.

I think what you could have done however is to refer to the documentation, such as that, and try to figure it out yourself. If the documentation is not clear then ask about that. Do you see anything in any documentation that indicates that the preceding code might be acceptable? If so then tell us what/where that is.

I recommend that you mostly use function declarations, with the occasional anonymous function as a function expression when suitable.

The above function as a function declaration is hard to muck up.

function greenEnergy(fueltype) {
    fueltype['Turbo Fuel'] = 'avocado oil';

In this example, you’ve changed 'Fuel Type' to fueltype for the parameter.

a) Why is that?
b) is fueltype the same thing as 'Fuel Type'?

What you attempted is completely broken and cannot work.

No, those are different things. The string cannot work as a reference.

let spaceship = {
‘Fuel Type’: ‘Turbo Fuel’,
homePlanet: ‘Earth’
let greenEnergy = (obj)=>{obj[‘Fuel Type’] = ‘Olive Oil’}
alert(spaceship[‘Fuel Type’] ) // alerts ‘Olive Oil’

A,B,C: True.

‘After an object’ is a misnomer there. {} designates an object literal. the var myObject does not indicate what is going to be assigned the the variable myObject (other than naming conventions). I could say var myObject = 4; and still be valid, if misleading.

E,F,G,H,I: true.
J: False. A key is a string. The key can reference a property (value) or a method (function).
K: Incomplete, see M.
L: Incomplete. color is a property of the object.
M,N,O: True.
P: True of an object literal, but not true of all properties of all Objects. This is a more advanced topic.
Q,R,S: True.
T: True if you’re talking about assigning a method. A property can be shorthanded with caveats:
the key to access the value is the same name as the variable that created it.
by definition of caveat #1, this only applies to variables. You can’t put a primative value in without a key.
by definition of caveat #2, you cannot use a reserved word in this manner.
U: True. If you don’t use parenthesis, you would get the function definition as a return.
V: True, but you have to use commas between all members of an object literal anyway, so this is no different.
W: True.
X: Yeah, see, this is what V should have said :stuck_out_tongue:
Y: See clairification of Z.
Z: True.

1 Like

A string or a number, right? And since a key can be a number that causes JavaScript objects to overlap with JavaScript arrays.

Nope. Anything not a string (or Symbol) is coerced to a string.

They overlap, but only insofar as the accessor is implicitly coerced to string before the evaluation is made.

Thanks for going through all of my assumptions m_hutley. It allows me to double check my foundations before moving forward.
True or false?:
a) spaceship is a variable but it’s also an object.

b) all of the words in light blue (passengers, telescope, yearBuilt…) are keys. What is written to the right of the colon : are all values.

c) Those values vary:
Line 2: the value is another object.

d) for line 4, 5 and 6: number, string, number for the values of those three keys.

a) spaceship is a variable that currently refers to an object. Keep in mind that nothing stops me from saying spaceship = 4 on the next line and changing what spaceship refers to.

b) True, though note that they’re not all at the same depth. Consider how you would reference 'Thomas' in your example, as opposed to 2018.

c) False. The value is an array. Answer the first part of my response to b).

d) True.

a) spaceship is a variable that currently refers to an object. Keep in mind that nothing stops me from saying spaceship = 4 on the next line and changing what spaceship refers to.

  1. Suppose I only wrote:
    let spaceship = 4 would you say that spaceship is a variable which refers to 4? I just want to make sure I’m clear about the terminology.

  2. The reason why you say it refers to an object is because of the curved braces after the assignment operator I believe.

  3. I’m not too sure how I would refer to ‘Thomas’ as I was fairly certain in was an object because of the curved braces in the array. I thought this was a way of indicating what’s inside the array is an object, not a simple array.

I think that in the case of passengers: [{name: 'Thomas'}] you have a key whose value is an array containing another key-value (name and Thomas)? Is that true or am I mistaken?

  1. I do agree they don’t all have the same depth however.

That seems irrelevant to me. The following produces two lines that are identical. I am not familiar with any other language where we can use array syntax to access an element with a non-numeric key.

a = { 1: "number", "2": "text" };
output.innerHTML = a["1"] + " " + a[2];
output.innerHTML += "<br>";
b = [ "number", "text" ];
output.innerHTML += b[0] + " " + b['1'];

Well with JavaScript, that comes from “Everything’s an object”.