3

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 accomplish 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?

4
  • what are you trying to accomplish? 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). Commented Jul 16, 2016 at 19:47
  • Read this, will help you slot, update m me if future assistant needed, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 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. 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. Commented Jul 16, 2016 at 19:57

1 Answer 1

5

Your original code is needlessly complicated 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 classes 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I thought I had needless code in there and that is what was probably the issue with my confusion.
Sorry, the sellCar() was part of my function and just added to the prototype that initialized methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.