JavaScript Practice Object Basics in JavaScript Practicing Object Basics Practice Adding a Property on the Fly

silva celso
PLUS
silva celso
Courses Plus Student 12,341 Points

Can someone tell why i can not assign the value in this property like such, characters: this.string.length

const myString = { string: "Programming with Treehouse is fun!", characters: this.string.length, countWords: function(){ const wordArray = this.string.split(' '); return wordArray.length; } }

const numWords = myString.countWords(); // To create a new property // myString.characters = myString.string.length;

console.log(myString.characters);

Vince Brown
Vince Brown
16,249 Points

Silva, In this case this is not what you think it is, Since the object is not created yet the reference to this is undefined.

You could achieve your desired result a few ways

You can start by doing the object step by step

const myString = {}
myString.string = "Programming with Treehouse is fun!"
myString.characters = myString.string.length
myString.countWords = function () {
    const wordArray = this.string.split(' '); 
    return wordArray.length;
}

OR

const myString = {
  string: "Programming with Treehouse is fun!",
  getStringLength: function() {
    // since the object will be created by the time the function runs you can refer to this within
    return this.string.length
  },
  countWords: function(){ 
    const wordArray = this.string.split(' '); 
    return wordArray.length; 
  } 
}

2 Answers

silva celso
PLUS
silva celso
Courses Plus Student 12,341 Points

If this is the issue why can I not assign the value to the characters property like such?

const myString = {
    string: "Programming with Treehouse is fun!",
        characters: myString.string.length,
    countWords: function(){
        const wordArray = this.string.split(' ');
        console.log(wordArray.length);
                return wordArray.length;

    }
}


console.log(myString.characters);
Vince Brown
Vince Brown
16,249 Points

Hey Silva, You are trying to reference myString for the characters property and at the time you are referencing it the myString object has not finished being created, so when you reference it is undefined.

The reason it works in my first example is because we are doing it step by step and not in one go. In the first example.

We create the object

const myString = {}

Then we add the string property to the myString Object which is already created.

myString.string = "Programming with Treehouse is fun!"

So far we have created the object and added the string property so if you run

console.log(myString.string)

You will get your expected output because the string property has been added to the object.

Since the object has been created and the string property added you can now use the string property as you expect.

myString.characters = myString.string.length
silva celso
PLUS
silva celso
Courses Plus Student 12,341 Points

It all makes sense now. Thank you Vince. It would be nice if i could chat with you directly. That should be a feature they should implement.

Vince Brown
Vince Brown
16,249 Points

Great to hear Silva glad I could help, can you please mark the initial answer as accepted in case anyone is browsing and finds the same issue they know their is a solution.