Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26. ES6 中的类是什么 #43

Open
webVueBlog opened this issue Jun 6, 2022 · 0 comments
Open

26. ES6 中的类是什么 #43

webVueBlog opened this issue Jun 6, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

在 ES6 中,Javascript 类主要是 JavaScript 现有的基于原型的继承的语法糖。例如,用函数表达式编写的基于原型的继承,如下所示,

function Bike(model, color) {
  this.model = model;
  this.color = color;
}

Bike.prototype.getDetails = function () {
  return this.model + " bike has" + this.color + " color";
};

而 ES6 类可以定义为替代

class Bike {
  constructor(color, model) {
    this.color = color;
    this.model = model;
  }

  getDetails() {
    return this.model + " bike has" + this.color + " color";
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant