admin管理员组文章数量:1400823
I am working on learning how to use es6 class
and need some help in understanding how this would be converted from a function
to a class
:
function MyModel(make, year) {
var carType = this;
carType.make = make;
// function that I have further in code
sellCar(carType, year);
}
What I am trying to acplish is something like this:
class MyModel {
constructor(make, year) {
this.make = make;
this.year = year;
sellCar(this.make, this.year);
}
}
What I get confused about is what I do about the reference I have to this
that I reference from the variable. Do I need that? I use that in other parts of my code, but would rather refactor to not do so.
The sticky point for me right now is assigning this
to carType. If I put the code below in my constructor
, how do I point a reference to this
from carType
?
I am working on learning how to use es6 class
and need some help in understanding how this would be converted from a function
to a class
:
function MyModel(make, year) {
var carType = this;
carType.make = make;
// function that I have further in code
sellCar(carType, year);
}
What I am trying to acplish is something like this:
class MyModel {
constructor(make, year) {
this.make = make;
this.year = year;
sellCar(this.make, this.year);
}
}
What I get confused about is what I do about the reference I have to this
that I reference from the variable. Do I need that? I use that in other parts of my code, but would rather refactor to not do so.
The sticky point for me right now is assigning this
to carType. If I put the code below in my constructor
, how do I point a reference to this
from carType
?
- what are you trying to acplish? make and year are most likely memeber variables, sellCar likely a method (although I would argue probably doesnt belong as a method inside of the class rather something that acts on an Object of that class). – akaphenom Commented Jul 16, 2016 at 19:47
- Read this, will help you slot, update m me if future assistant needed, developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Hasan A Yousef Commented Jul 16, 2016 at 19:52
-
You can just use
class MyModel { constructor(make, year) { /* same function body here */ }}
. No need to convert variables to properties. – Oriol Commented Jul 16, 2016 at 19:55 - @akaphenom Please see EDIT and let me know if that helps you understand my confusion a bit. Just trying to get to a point where I understand if it makes sense to reassign this to a variable. – pertrai1 Commented Jul 16, 2016 at 19:57
1 Answer
Reset to default 5Your original code is needlessly plicated and doesn't make sense
function MyModel(make, year) {
var carType = this;
carType.make = make;
// function that I have further in code
sellCar(carType, year);
}
It could be written as
function MyModel(make, year) {
this.make = make;
sellCar(this, year);
}
In ES6, it's a trivial transform
class MyModel {
constructor (make, year) {
this.make = make;
sellCar(this, year);
}
}
ES6 class
es are just syntactic sugar, so the functionality is going to be identical (provided you always invoke the constructor with the new
keyword (which you should))
But what is sellCar
? The return value is discarded so I have to believe that sellCar
has some other kind of side effect.
本文标签: javascriptConverting function to es6 classStack Overflow
版权声明:本文标题:javascript - Converting function to es6 class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744177429a2594067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论