class User{
constructor(username,description){
this.username = username
this.description = description
}
printInfo(info){
if (info in this){
return info
}
}
}
let newUser = new User("testUsername","testDescription")
newUser.printInfo(username)
When I try this, it gives me an error on line 17 about Uncaught ReferenceError
.
You forgot the quotes when passing the username property name. The passed username
argument has to be a string newUser.printInfo("username")
.
Without the quotes, it will try to reference a (non-existing) global variable named username
.
Note that your printInfo
function will just return the name of the property (same as the parameter), not the actual value. If you want to return the username value you have to access that key as this[info]
:
...
printInfo(info){
if (info in this){
return this[info];
}
}