What's the difference between the way js creates classes function and class?

ask for an explanation because I learned es6, directly at the beginning. Es6, has always been in the class way of ES6, and function has not been used much.


The class of

ES6 can be seen as just a grammatical sugar

//
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}

is equivalent to the following ways

function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return '(' + this.x + ', ' + this.y + ')';
};

early JS did not have class syntax, so creating objects is implemented using new (function) .
now has class , extends syntax, which helps to quickly build an object without having to manually specify things like prototype .
you can think that class is a grammatical sugar, because its function can be achieved without using the class keyword


I say personal understanding, but I am not very familiar with this piece of js, nor have I read relevant books. It can only be said that it is a personal one-sided understanding and accumulation. There is no difference between class and function in
js, because when you use it, you will find that whether you want to define a class or a function, the final use is the same. New is called because function is both a method and an object, while class is an object and a combination of methods. Within function, you can use the properties of the object to create new methods and new properties for itself to call. And class, not to mention.
personally, if we have to make a distinction, it should be the rationality of the package (easy to maintain and easy to use).

Menu