When vars are replaced with const the code is producing an error. I run this code in the chrome browser console. I tried initializing the variable with let but that is producing an error as well. I ve checked docs on variables before asking this question and I know I am missing something crucial here.
class User {
constructor(email, name){
this.email = email;
this.name = name;
this.score = 0;
}
login(){
console.log(this.email, 'just logged in');
return this;
}
logout(){
console.log(this.email, 'just logged out');
return this;
}
updateScore(){
this.score++;
console.log(this.email, 'score is now', this.score);
return this;
}
}
class Admin extends User {
deleteUser(user){
users = users.filter(u => {
return u.email != user.email
});
}
}
//const userOne = new User ('ryu@ninjas.com', 'Ryu');
var userOne = new User('ryu@ninjas.com', 'Ryu');
var userTwo = new User('yoshi@mariokorp.com', 'Yoshi');
var admin = new Admin('shaun@ninjas.com', 'Shaun');
var users = [userOne, userTwo, admin];
//const users = [userOne, userTwo, admin];
admin.deleteUser(userTwo);
console.log(users);
That happen Because you have two variables userOne with the same name.
With let and const you can't declare more than one variable with the same name in the same scope, but with var it will not reproduce an error.
Also, because you are re-assing the const value of users, declare it with let not const
class User {
constructor(email, name){
this.email = email;
this.name = name;
this.score = 0;
}
login(){
console.log(this.email, 'just logged in');
return this;
}
logout(){
console.log(this.email, 'just logged out');
return this;
}
updateScore(){
this.score++;
console.log(this.email, 'score is now', this.score);
return this;
}
}
class Admin extends User {
deleteUser(user){
users = users.filter(u => {
return u.email != user.email
});
}
}
const userOne = new User('ryu@ninjas.com', 'Ryu');
const userTwo = new User('yoshi@mariokorp.com', 'Yoshi');
const admin = new Admin('shaun@ninjas.com', 'Shaun');
let users = [userOne, userTwo, admin];
admin.deleteUser(userTwo);
console.log(users)