Home > database >  Not defined object
Not defined object

Time:10-17

I'm trying to get my object to print wolverine's email and name in the console. However, wolverine is showing up in the console as "not defined". How is this possible when it's a part of a class called new User? Any help would be appreciated.

class User {
  constructor(email, name) {
    this.email = email;
    this.name = name;
  }
}
let userOne = new User('[email protected]', wolverine);
let userTwo = new User('[email protected]', sabertooth);

console.log(userOne);
console.log(userTwo);

CodePudding user response:

That is happening you are passing wolverine and sabertooth as variables, not as strings.

Correct usage:

let userOne = new User('[email protected]', 'wolverine');
let userTwo = new User('[email protected]', 'sabertooth');

CodePudding user response:

You're getting undefined (or, in strict mode, an error) because the variables wolverine and sabertooth aren't assigned, so their value is... undefined.

You are probably looking to pass the names as strings too:

let userOne = new User('[email protected]', 'wolverine');
let userTwo = new User('[email protected]', 'sabertooth');

Alternatively, you may be looking to have the names in variables first, then pass the variables in:

let nameOfGuyWithHandBlades = 'wolverine';
let totallyATiger = 'sabertooth';
let userOne = new User('[email protected]', nameOfGuyWithHandBlades);
let userTwo = new User('[email protected]', totallyATiger);

CodePudding user response:

Passing wolverine like that treats it as a variable, and it clearly can't find it.

If it's a string, like you did with the email, you need quotes '.

let userOne = new User('[email protected]', 'wolverine');
let userTwo = new User('[email protected]', 'sabertooth')
  • Related