Beginner Questions about Objects

Yet as has been stated previously, in JavaScript numbers are not objects. Numbers are objects in C# so 4.ToString() works in C# (as in Numbers Are Objects) but 4.toString() is not valid in JavaScript.

However, Number(4) is also a number, and Number(4).toString() does work.

It doesn’t even have to be Number(4) either. Just putting a number in parenthesis lets it be treated as an object.

(4).toString() // "4"

I recommend that you stop trying to apply other coding languages to JavaScript as if they were JavaScript.

The English language sounds pretty good when spoken with a French accent. Programming languages don’t.

2 Likes

That is interesting. It contradicts what was said previously.

If I have ever said or implied that something is in JavaScript that is not then I apologize, I certainly did not intend to.

1 Like

ironically, it depends on the context.

4 * "4" will implicitly coerce the string to a number.
4 + "4" will instead coerce the number to a string. (byproduct of using + as string concatenation as well as addition)
4.toString has no definition (primitives do not have methods.)
(4).toString does (explicit shorthand to Number object)
of course, there’s the impliicit coercion we all use quite often in most languages:
if (2) { (implicit coercion to boolean)

This doesn’t work only because the period is used for decimals though – as primitives are getting auto-boxed when required, all these would work:

  • (4).toString()
  • 4..toString()
  • 4['toString']()

Could you start a different thread to discuss this please? This isn’t really beginner material.

1 Like

The terminology is a bit nitpicky. The point was more to emphasize that it’s currently an object, and that variables reference points in memory, rather than actually ‘are’ the object. Unless you’re being very pedantic (as i get sometimes), saying it ‘is’ an object is fine, given that you remember the ephemeral nature of variables.

Spaceship is an object.
Passengers is a property of the Spaceship object.
Passengers contains an array.
That array has one element.
The element at the 0th position of the array is an object.
Name is a property of that object.
Name contains a string value, “Thomas”.

Follow the onion all the way down to the core. Every time you hit a symbol, you’ve got a new line.

The reference for ‘Thomas’ in this case would be spaceship.passengers[0].name

The terminology is a bit nitpicky. The point was more to emphasize that it’s currently an object, and that variables reference points in memory, rather than actually ‘are’ the object. Unless you’re being very pedantic (as i get sometimes), saying it ‘is’ an object is fine, given that you remember the ephemeral nature of variables.

I find being pedantic is actually useful when it comes to programming syntax as vague terminology almost systematically brings up confusion in the long term.

I. From what I’ve learned so far, the keyword let is used to declare a variable. So at line 1, let spaceship the word that follows let can only be a variable.
image
The idea that a variable points to a quote “point in memory” has been brought up a few times. Is it necessary for me to understand this concept or is it more like extra information which is good to know but not that relevant when it comes to JavaScript?

II.
You know how an ‘adjective’ is a word and a ‘verb’ is a word but a verb is not an adjective and an adjective is not a word. I think this should explain what I’m trying to say:

Each entry in the picture above is a fairly clear concept distinct from each other. A closer example to the topic at hand would be:


They are words with distinct functions (used generally here) while sharing a general ‘category’ for lack of a better word.

In the case of let spaceship, am I right to say that spaceship has multiple characteristics:

  1. it’s a variable
  2. it’s an object

How accurate would that be?

III.

Spaceship is an object.
Passengers is a property of the Spaceship object.

Is passengers also an object? It’s not a string as it doesn’t have ’ ’ and it it’s not one of the 6 primitives data types. So is it an object?

Would I be right to say:
Passenger has the following overlapping characteristics:

  1. property of spaceship object.
  2. is a key
  3. has a value of [{Thomas}]

As of now, the terminology is nebulous in my mind. I need to sort this out.

IV.
Is this right:
image
In the sense that what follows an object is a list of properties of that object?
In the case of spaceship it’s properties would be:

  1. Passenger
  2. Telescope
  3. Crew

Am I right about all this?

it’s currently referencing an object.

Here’s the problem with the diagram: Things in programming languages aren’t necessarily distinct. They’re contextual.

let is distinct from var.”… Except if you’re in the global space, in which case they’re equivalent and identical.

This is false. passenger has a value of an Array reference. What’s contained inside that reference is not itself the value of passenger.

True. Those properties may contain values that themselves have properties, but those are the tree properties of spaceship at present.

Ok.

I. So in that case, what is the difference between a property and a key-value pair? There is none right, they are simply overlapping.

II. spaceship is a variable which has three properties. Each property has its de facto key-value pair. A key always comes with a value which is why they’re called a pair.

Like this:
image

III.

This is false. passenger has a value of an Array reference. What’s contained inside that reference is not itself the value of passenger.

What is it then?

IV.
image

Am I right:
a. The curved braces are used in the array to introduce an object as an array element. This is their only purpose there.
b. key: name
c. value: ‘Thomas’

Pedantically? key-value pair describes the format. property is the thing. Practically? Noone can be faffed to type key-value pair over and over.

each property is a key-value pair, in the same way that 4 is a number.
A defined property always has a value, even if that value is undefined.

What is what? the value of passenger? A memory reference, which is pointing at an array.

The purpose of the curly braces here is the same as the curly braces after the = in the spaceship definition. Their purpose is to declare an object literal. Their position inside the square brackets tells you it’s a part of the array, and the fact that it’s the first thing inside those square brackets tells you it’s the 0th element of said array.
The square brackets, in this context, are defining an array (rather than indexing an existing one).

I can define an array with anything, including other arrays.

let myArray = [1,"quack",undefined,[],{},[{name: "Bob",age:32},{name: "Katie", age:16},{name: "Jim", hobbies: ["biking","swimming"]}]]`
1 Like

Very good reply, clearly explained, well done.

Each property is a key-value pair, in the same way that 4 is a number.
A defined property always has a value, even if that value is undefined .

Let’s go back the previously displayed picture:
image

telescope is a property. What is its value? Three more properties? Is that it?
image

It’s simple like this:
yearBuilt (that’s the property/key) : 2018 (that’s the value). I want to make sure about the situation above.

every value in an object that is not a primitive is a memory reference to the object that it’s pointing at.
an object literal does not have an intrinsic ‘value’.
{ "wark": 1, "quack": "squark", "pomfrey":"stupendous"}
what is the value of this object? Well you could say it was 1. Or “squark” , or “stupdendous”… but is it? Not really. It’s at best ambiguous, but in practicality, there isnt a value to the object itself.

I don’t understand that. What ‘memory reference’? Any sort of link would be useful.

an object literal does not have an intrinsic ‘value’.

Ok, that’s what I wanted to know. An object literal just tells you on incoming properties in between the curved braces and what comes before the curved braces is an object.

You wouldn’t know spaceship is an object if there wasn’t a “declaration” of an incoming object literal would you?

Are all the properties of the spaceship object also objects on their own?

This link may help.

We’re wandering deeper than Beginner level here, though.

an object literal gives a shell of an object without a defined class or type. It is, effectively, a prototype Object.

In this case, yes. Which is not to say that all properties of objects are objects: the name property of the passengers’ zeroth index object has “Thomas”, a primitive, as its value.

Liking the article so far, I find it well written and accessible.

Variables that are assigned a non-primitive value are given a reference to that value. That reference points to the object’s location in memory. The variables don’t actually contain the value.

As opposed to:

So variables play a different role depending on whether they are assigned to primitives or to objects.

I wrote the following based on @Shaka_Zorba comment in the thread:
image

and it worked. I suspected line 8 & 9 weren’t necessary because they were calls so I removed them and it worked as well, although I think it’s clearer like this to me:
image

At a glance, it seems fairly straightforward.

The second step I managed as well:
image
image
Although to be honest it was similar to step 1.

Which brings me to my next question.

How do you call a parameter which is a string? That never seems to work.

For the third step, I need to call my two newly created functions.
So I wrote:
remotelyDisable.obj.disabled;
…which I think is right.
It’s the other function call which I find harder to do.

I tried:
greenEnergy.obj.[{'Fuel Type'}] but no.

greenEnergy.obj.'Fuel Type' but no although I was fairly sure that was wrong because the brackets were missing but the console was telling there was a problem with the brackets.

greenEnergy.obj.['Fuel Type'] but that didn’t work either, pointing to the brackets as the problem.

Out of curiosity, I tried removing ‘Fuel Type’ entirely to see what would happen and it told me that:
image
So that’s wrong too then.

So I decided to go and check how to call nested objects in order to do this:
image
So according to this, I need to put my array right next to the property. The problem was likely the dot operator I put before my array:
greenEnergy.obj['Fuel Type'] but that didn’t work either. It’s weird, I did the exact same thing as in the example.

What am I doing wrong here?

Much of what you have been asking about is not beginner material. And very, very much should be split up into multiple discussions. This is a symptom of having one discussion that discusses many things.

There are variations in the way people think. Some people do not need detailed explanations and some people do. There are advantages to having detailed explanations and advantages to not needing them. Most people get the concept of variables easier than you. I am not saying either is best; you are just different. So it is not clear what would help you best. For most of us the concept of variables is quite clear (at least for me). It is difficult for us to understand you enough to know how to explain it.

Let us say you have a box and you put books in it. You (not us) would say the box is books but in fact it is a box of books. You can take the books out and put food in the box. The box is no longer books, it is a box of food.

I was too curious and I checked the solution. I was too quick and should’ve read the instructions better. I had to call functions, not declare objects!
image

So the next part is… looping through objects. Loops are the thing I’m having trouble understanding so far, it’s hopefully easier to understand when it is applied to objects!

Yes I know that numbers can be put into a Number object. So what you are saying is that the number alone is not an object, that it must be put into / coerced into a Number object.