Constructing an object name

Tell us what’s happening:
In the below code is the name of the object I’ve created “carrot” or “name”?

I am a little bit confused as name is a variable representing the string “carrot”, but does the computer just read the variable name when assigning the object private variable and the string when assigning the value?

I hope the way I asked this question makes sense.

  **Your code so far**

// Only change code below this line
class Vegetable {
constructor(vegName) {
  this.vegName = vegName;
// Only change code above this line

const carrot = new Vegetable('carrot');
console.log(; // Should display 'carrot'

If I changed the code to

constructor(vegName) {
  this.vegName = vegName;

The value of this.vegName would still be carrot right? It would just change the object private variable name from name to vegName?

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36

Challenge: Use class Syntax to Define a Constructor Function

Link to the challenge:

what happens when you call carrot.vegName? (ignore this, i didnt see full code first!! :grin:)

and also what do you mean by ’ object private variable’? thanks :slight_smile:

I mean the variables that an object has.

Object {
var1: 123,
var2: 345

What are var1 and var2 called?

they are just object properties (i.e. prop: value)

for further info, feel free to read up on this article

So whatever variable I give to the constructor determines what the property will be called, and the value of the variable becomes the property value. Is that correct?

you’re passing in argument into your constructor function

and yeah, according to this snippet, thats how it should be!!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.