admin管理员组文章数量:1405170
I know that ES6 solved a lot of problems that existed with the this
keyword in ES5, such as arrow functions and classes.
My question relates to the usage of this
in the context of an ES6 class and why it has to be written explicitly. I was originally a Java developer and I e from a world where the following lines of code were perfectly natural.
class Person {
private String myName;
public Person() {
myName = "Heisenberg";
}
public void sayMyName() {
System.out.println("My name is " + myName);
}
}
The piler will always refer to the value of the field myName
, unless it has a local variable with the name myName
defined in the scope of the method.
However, once we convert this code to ES6:
class Person {
constructor() {
this.myName = "Heisenberg";
}
sayMyName() {
console.log(`My name is ${myName}`);
}
}
This won't work and it will throw an Uncaught ReferenceError: myName is not defined
. The only way to fix this is to throw in an explicit this
reference:
console.log(`My name is ${this.myName}`)
I understand the need for this
in the constructor since ES6 classes don't allow your fields to be defined outside of the constructor, but I don't understand why the Javascript engines can't (or won't, because of the standard) do the same as Java pilers can in the case of sayMyName
I know that ES6 solved a lot of problems that existed with the this
keyword in ES5, such as arrow functions and classes.
My question relates to the usage of this
in the context of an ES6 class and why it has to be written explicitly. I was originally a Java developer and I e from a world where the following lines of code were perfectly natural.
class Person {
private String myName;
public Person() {
myName = "Heisenberg";
}
public void sayMyName() {
System.out.println("My name is " + myName);
}
}
The piler will always refer to the value of the field myName
, unless it has a local variable with the name myName
defined in the scope of the method.
However, once we convert this code to ES6:
class Person {
constructor() {
this.myName = "Heisenberg";
}
sayMyName() {
console.log(`My name is ${myName}`);
}
}
This won't work and it will throw an Uncaught ReferenceError: myName is not defined
. The only way to fix this is to throw in an explicit this
reference:
console.log(`My name is ${this.myName}`)
I understand the need for this
in the constructor since ES6 classes don't allow your fields to be defined outside of the constructor, but I don't understand why the Javascript engines can't (or won't, because of the standard) do the same as Java pilers can in the case of sayMyName
- 2 So you basically are asking why two mostly pletely different languages behave differently? Note that the "java" in "javascript" has nothing to do with the programming language Java. – luk2302 Commented Jan 3, 2017 at 14:20
- 2 @luk2302 - If you said the languages have basically no relationship, that's true. But when you say "the 'java' in 'javascript' has nothing to do with the programming language Java", that's false as you could verify with a quick google search. From wikipedia: "... upon receiving a trademark license from Sun, the name JavaScript was adopted. This was somewhat of a marketing move at the time, with Java being very popular around then" – Mark Adelsberger Commented Jan 3, 2017 at 14:43
- 2 Not only are you paring different languages but also different concepts. "I understand the need for this in the constructor" Why should the constructor function be handled differently? – a better oliver Commented Jan 3, 2017 at 15:07
- 3 It would make the language inconsistent and more plex since now we would need some other construct to refer to variables in outer scopes (instead of members). However, questions about design decisions for the language are not a good fit for SO in general. You might want to ask on esdiscuss instead. – Felix Kling Commented Jan 3, 2017 at 15:22
- @FelixKling I guess your ment is a better answer than the current one, if you post an answer I'll be happy to accept it – Robin-Hoodie Commented Jan 4, 2017 at 8:13
1 Answer
Reset to default 8Maybe I will not directly answer your question, but I'll try to direct the way you should think about JS class
keyword.
Under the cover there is no magic about it. Basically it's a syntatic sugar over prototypal inheritance that was since the beginning of JavaScript.
To read more about classes in JS click here and here.
As of why you need explicitly write this
is that because in JS it's always context sensitive, so you should refer to exact object. Read more.
本文标签: javascriptWhy is quotthisquot in an ES6 class not implicitStack Overflow
版权声明:本文标题:javascript - Why is "this" in an ES6 class not implicit? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744882691a2630300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论