User error strikes again! Thanks
WHY THIS WOULD NOT PRINT ME ANY RESULTS?
const robot = {
model: '1E78V2',
energyLevel: 100,
provideInfo() {
console.log(`I am ${this.model} and my current energy level is ${this.energyLevel}.`)
}
};
It should give you output if you invoke the method after the last closing brace…
robot.provideInfo()
Hi, I’m enjoying ‘this’, I think I get it. But I’d love to know why my code for this exercise returns an extra ‘undefined’ even though I’ve got my tick for this exercise:
const robot = {
model: '1E78V2',
energyLevel: 100,
provideInfo() {
console.log(`I am ${this.model} and my current energy level is ${this.energyLevel}`);
}
};
console.log(robot.provideInfo());
//I am 1E78V2 and my current energy level is 100
undefined
This is a common error… Double logging. robot.provideInfo()
has no return, so when logging that expression the only thing left to log is the default return value, undefined
.
Try changing your method to return
the string, rather than logging it. Then your last line will log that string, and not the default.
I had the same problem. pressed the ‘code refresh’ icon, and typed it out (where I had pasted and edited the example text before).
worked for me!
Thanks, that worked:
const robot = {
model: '1E78V2',
energyLevel: 100,
provideInfo() {
return(`I am ${this.model} and my current energy level is ${this.energyLevel}`);
}
};
console.log(robot.provideInfo()); //I am 1E78V2 and my current energy level is 100
I do feel I do a lot of guessing whether to return or console.log. But I guess not logging twice is a start!
I finished the Meal Maker project. Debugging it made me realize there’s a lot I don’t understand about when to use the this
keyword.
In particular, the following code was resulting in my project returning undefined, until I removed the this
keyword.
addDishToCourse (courseName, dishName, dishPrice) {
const dish = {
name: this.dishName,
price: this.dishPrice
}
I can see now that all I was doing here is constructing an object, so no need to refer back to the calling object, but I wondered if there is a hard and fast rule to help me understand the logic of this
?
Thank you!
Quoting from MDN,
The
this
keyword refers to a special property of an execution context.
Expressions and Operators
Context refers to within the block, similar to scope, but also confined to the current owner (calling) object.
obj {
_prop: 'value',
get prop () {
return this._prop;
},
set prop(newValue) {
this._prop = newValue;
}
}
Above, the context is obj
, so this
refers to that object.
console.log(obj.prop) // value
obj.prop = 'new value'
console.log(obj.prop) // new value
Hello everybody I try to resolve the exercise with ‘this’ (question 2) but I am getting an error. Can someone help me please?
const robot = {
model: '1E78V2',
energyLevel: 100,
provideInfo() {
return `I am ${this.model} and my current energy level is ${this.energyLevel}.`;}
};
Hi @akane96, your code matches what I solved this lesson with, the only difference i see is your semi-colon at the end of your return statement – however, I don’t think this would cause an error.
Can you describe what error message you are seeing? Is it a JavaScript error or a Codecademy error?
I had the same problem, I have tried all versions but nothing worked. For me the problem was the provideInfo() method and it was complaining about that very word provideInfo(). Not sure what was the problem, I think its the usual BS that is a bit frustrating. I think someone should check out these exercises once in a while.
When I looked at your coding, I didn’t see the back ticks to indicate a string like so:
returnI am ${this.model} and my current energy level is ${this.energyLevel}.
Hi, you might be missing a closing curly brace.
I count 3 open curly braces and 2 close curly braces
Hi, I don’t understand why there is a semi-colon after your console.log statement. Could that be the issue?
Hey, good question, Im not sure if that was the problem. Actually it complains about line 4 but that could be for another reason too.
I’ve definitely encountered a lot of complaining! It brings out my determination.
Well going back to my complaints, yeah it does the same thing to me too. I guess I break the records with my complaints in two topics, the use of words and length.
Why was it decided that this
is required to access object properties from within the object? What is the advantage of having this requirement?
this
is the property that identifies the object instance within its own methods. Yes, we could just use the name of the object, but then we cannot clone it.
To clone an object in ES2018 we can use the new spread operator.
obj2 = { ...obj1 }
It will be a shallow copy but any methods will work on the new object if we use this
and not the name of the object.