Chaz Carothers
Chaz Carothers

Reputation: 188

Not defined object

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);

Upvotes: -1

Views: 778

Answers (3)

Balastrong
Balastrong

Reputation: 4464

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')

Upvotes: 1

AKX
AKX

Reputation: 169022

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);

Upvotes: 1

Arnas Savickas
Arnas Savickas

Reputation: 384

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');

Upvotes: 1

Related Questions