1
class User{
    constructor(username,email){
        this.username= username,
        this.email = email,
        this.score = 0
    }
    a = [this.email]  // showing undefined 
    login(){
        console.log(`you have logged in ${this.username}`);
        return this;
    }
    logout(){
        console.log(`you have logout ${this.username}`);
        return this;
    }
    incScore() {
        this.score +=1;
        console.log(`The ${this.username} have scored ${this.score}`)
        return this;
    }


}

const userOne = new User('mdvenkatesh','mdv@gmail.com');
console.log(userOne)

// but check it here 


class User{
    constructor(username,email){
        this.username= username,
        this.email = email,
        this.score = 0
    }
    a = [this]  // showing the complete user data  
    login(){
        console.log(`you have logged in ${this.username}`);
        return this;
    }
    logout(){
        console.log(`you have logout ${this.username}`);
        return this;
    }
    incScore() {
        this.score +=1;
        console.log(`The ${this.username} have scored ${this.score}`)
        return this;
    }


}

After my class i declared a array a and intilized with the this keyword o/p

a: Array(1)
0: User {a: Array(1), username: "mdvenkatesh", email: "mdv@gmail.com", score: 0}
length: 1
__proto__: Array(0)
email: "mdv@gmail.com"
score: 0

username: "mdvenkatesh"

but when i tryed with user.name it is showing undefined the o/p is following

User {a: Array(1), username: "mdvenkatesh", email: "mdv@gmail.com", score: 0}
a: [undefined]
email: "mdv@gmail.com"
score: 0
username: "mdvenkatesh"
__proto__: Object

i cantunderstand why you may thing why schould even intilize a varable outside method i want to know how a is treated here i am thinking it is proprtey of the class (plese correct me if i am wrong )

2)why this.username is showing undefined when user is show the data plese help me to know

2 Answers2

2

When you do a = [ this.username ], username is a string and hence its passed via value.

When you do a = [ this ], this being an object is passes via reference. So any changes made to this will be reflected in a.


here i am thinking it is property of the class

Your assumption is correct. But properties are initialized during compile-time and are updated in constructor. This way is used to set default values/ static values.

If you wish to have a property that gets you value of a property, use getter function.


Following is the illustration of above points:

  • I have created a getter function called userName that returns necessary value.
  • I have also moved this.scope = 0 outside constructor as this is a default value.

class User {
  score = 0;
  constructor(username, email) {
    this.username = username;
    this.email = email;
  }
  get userName() {
    return this.username;
  }
  login() {
    console.log(`you have logged in ${this.username}`);
    return this;
  }
  logout() {
    console.log(`you have logout ${this.username}`);
    return this;
  }
  incScore() {
    this.score += 1;
    console.log(`The ${this.username} have scored ${this.score}`)
    return this;
  }
}

const user = new User('foo', 'foo@bar.com');
console.log(user.userName)
user.incScore();

Renerences:

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Thanks but a = [ this.username ] is alredy intilized to some username why it is showing undefined – mdvenkatesh nuhk Aug 12 '19 at 06:16
  • 1
    @mdvenkateshnuhk Its initialized in constructor but `a` is being assigned value during class definition phase. Anything outside constructor is assumed to be a part of class and is processed during creating the class itself, including properties. But properties are overridden in constructor. This is the ideal flow/behavior. Hope it clarifies your query – Rajesh Aug 12 '19 at 06:39
  • thanks Very helpful and update these line your answer and plese link any documention which gives more information – mdvenkatesh nuhk Aug 12 '19 at 07:01
  • Add this is a reference type – mdvenkatesh nuhk Aug 12 '19 at 09:47
0
a = [this]  // you declared this to var a then use a after a = [this] use like below
    login(){
        return console.log(`you have logged in ${a.username}`);
    }
and try user.username 
User {a: Array(1), username: "mdvenkatesh", email: "mdv@gmail.com", score: 0}
user.name not find

please check example in below link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Vaish
  • 21
  • 7