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
usernameargument has to be a stringnewUser.printInfo("username").Without the quotes, it will try to reference a (non-existing) global variable named
username.Note that your
printInfofunction 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 asthis[info]: