Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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

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

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.