[英]this scope inside the class after the constructor
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,并插入了this关键字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" 用户名:“ mdvenkatesh”
but when i tryed with user.name it is showing undefined the o/p is following 但是当我尝试使用user.name时,它显示为未定义,后面是o / p
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)为什么this.username在显示用户数据时显示未定义,请帮助我知道
When you do a = [ this.username ]
, username
is a string and hence its passed via value. 当您执行a = [ this.username ]
, username
是一个字符串,因此它是通过值传递的。
When you do a = [ this ]
, this
being an object is passes via reference. 当您执行a = [ this ]
, this
是一个对象,通过引用传递。 So any changes made to this
will be reflected in a
. 因此,要进行任何更改this
将反映在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. 如果希望拥有一个可以获取属性价值的属性,请使用getter
函数。
Following is the illustration of above points: 以下是以上几点的说明:
getter
function called userName
that returns necessary value. 我创建了一个名为userName
的getter
函数,该函数返回必要的值。 this.scope = 0
outside constructor as this is a default value. 我也已将this.scope = 0
移到构造函数之外,因为这是默认值。 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();
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 请在下面的链接中查看示例https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.