0

Is there any difference between the two methods of setting up a class in javascript:

Example 1:

class Friend {
  constructor(name) {
    this.name = name;
  }
}

var friendOne = new Friend('John');

Example 2:

function Friend(name){
  this.name = name;
}

var friendOne = new Friend('John');

Both of these examples create:

// Friend.prototype
// {constructor: ƒ}

So my question is related to what approach is best for creating class based inheritance with Javascript?

Thanks

jremi
  • 2,879
  • 3
  • 26
  • 33
  • 1
    This gets asked a lot, for example https://stackoverflow.com/questions/36099721/javascript-what-is-the-difference-between-function-and-class – Ray Toal Mar 19 '18 at 01:51
  • Perfect. I will review this. That is what I needed. – jremi Mar 19 '18 at 01:54
  • Possible duplicate of [Javascript: What is the difference between Function and Class](https://stackoverflow.com/questions/36099721/javascript-what-is-the-difference-between-function-and-class) – ShadowRanger Mar 19 '18 at 02:01

1 Answers1

0

I think there is no obvious differences for them. compare with "Function" constructor, "class" just syntax sugar, more semantic.

aaron.xiao
  • 198
  • 8