Object method not working

Hi, so if you see my image, for some my method is not working, I have already looked online (Checked few websites, w3schools had an example, this is exactly that, but instead of const object{...} I did Function Object(args){methods and properties}) If I just look for the property using console.log(admin.getAccesKey) it claims it is undefined.... Could someone please point out to me what I did wrong? I feel like it'd probably be something stupidly small but I just can't seem to figure it out This is all there is to the code btw, I am simply testing it out first before going on to the next step...But on step 1 its already going wrong xd
No description
3 Replies
Brightie
Brightie9mo ago
So yeah, I can get everything but the method What is making my method nonexistent to my object?
No description
Brightie
Brightie9mo ago
Okay, I am mad... So I added "this." before my method and it is working now, for crying out loud... but I thought you didn't have to add "this." for the methods ?
MarkBoots
MarkBoots9mo ago
with object constructors you do need the this. for methods with classes you don't have to
class AccesKey {
constructor(username, pp) {
this.username = username;
this.pp = pp;
}
getAccesKey() {
return `username: ${this.username} \n password: ${this.pp}`
}
}

const admin = new AccesKey('hello', 'world');
console.log(admin.getAccesKey())
/*
"username: hello
"pp: world
*/
class AccesKey {
constructor(username, pp) {
this.username = username;
this.pp = pp;
}
getAccesKey() {
return `username: ${this.username} \n password: ${this.pp}`
}
}

const admin = new AccesKey('hello', 'world');
console.log(admin.getAccesKey())
/*
"username: hello
"pp: world
*/