经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
js学习笔记之class类、super和extends关键词
来源:jb51  时间:2021/8/9 13:56:53  对本文有异议

前言

JavaScript 语言在ES6中引入了 class 这一个关键字,在学习面试的中,经常会遇到面试官问到谈一下你对 ES6 中class的认识,同时我们的代码中如何去使用这个关键字,使用这个关键字需要注意什么,这篇来总结一下相关知识点。

正文

1.es6之前创建对象

先来看下es6之前我们要想创建一个对象,只能通过构造函数的方式来创建,将静态方法添加在原型上面使得每一个实例能够调用该方法。

  1. function Person(name, age) {
  2. this.name = name
  3. this.age = age
  4. Person.prototype.sayHello = function () {
  5. return "hello," + this.name + ",早上好"
  6. }
  7. }
  8. let person = new Person("serendipity", 18)
  9. console.log(person.sayHello())//hello,serendipity,早上好
  10. console.log(person instanceof Person);//true
  11. console.log(person instanceof Object);//true

2.es6之后class的声明

类是用于创建对象的模板,他们用代码封装数据以处理该数据。js中的 class 类建立在原型之上,但也具有某些语法和语义与ES5类相似语义共享。

实际上,类是一种特殊的函数,就像定义函数声明和函数表达式一样,类的语法也有两个部分组成:类声明和类表达式。

  1. class Person {
  2. constructor(name, age) {//自有属性,该属性出现在实例上,只能在类的构造器或者方法内部进行创建
  3. this.name = name
  4. this.age = age
  5. }
  6. sayHello() {//等价于Perosn.prototype.sayHello
  7. return `hello,${this.name},早上好`
  8. }
  9. }
  10. let person = new Person("serendipity", 18)
  11. console.log(person.sayHello());//hello,serendipity,早上好
  12. console.log(person instanceof Person);//true
  13. console.log(person instanceof Object);//true
  14. console.log(typeof Person);//function
  15. console.log(typeof Person.prototype.sayHello);//function

类声明允许在class中使用 constructor 方法定义一个构造器,而不需要定义专门的构造方法来当构造器使用。

class 类的语法与普通es5之前的函数语法相似,但是还存在一些特性需要注意:

  (1)类的声明不会被提升,类的声明行为和 let 相似,因此执行时类会存在暂时性死区;

  (2)类中所有代码自动运行在严格模式下,且改严格模式无法退出

  (3) 类中所有方法都是不可枚举的,普通自定义方法只有通过 object.defineProperty() 才能将方法定义为不可枚举

  (4)类中的所有方法内部都没有 [[construct]] ,因此使用new 来调用他们会抛出错误

  (5)调用类构造器时不使用 new 会抛出错误

  (6)试图在类的方法内部重写类名会抛出错误

将上面的代码转换为ES5之前的写法如下:

  1. let PersonClass = (function () {
  2. "use strict"
  3. const PersonClass = function (name, age) {
  4. // 判断是否被new调用构造函数
  5. if (typeof new.target === "undefined") {
  6. throw new Error("Constructor must be call with new.")
  7. }
  8. this.name = name
  9. this.age = age
  10. }
  11. Object.defineProperty(PersonClass.prototype, "sayHello", {
  12. value: function () {
  13. if (typeof new.target !== "undefined") {//保正调用时没有使用new
  14. throw new Error("Method cannot be called with new.")
  15. }
  16. return "hello," + this.name + ",早上好!"
  17. },
  18. enumerable: false,
  19. configurable: true,
  20. writable: true
  21. })
  22. return PersonClass
  23. })()
  24. var personClass = new PersonClass("serendipity", 18)
  25. console.log(personClass.name);//serendipity
  26. console.log(personClass.sayHello());///hello,serendipity,早上好!

两个PersonClass 声明,一个在外部作用域的 let 声明,另一个在立即执行函数内部的 const 声明,这就是为何类的方法不能对类名进行重写,而类的外部的代码则被允许。同时,只在类的内部类名才被视为使用了const声明,这意味着你可以在外部(相当于let)重写类名,但是不能在类的方法内部这么写。

3.类的继承

ES6之前的继承方式主要通过构造函数和原型链组合的方式来实现继承,具体代码如下:

  1. function Rectangle(length, width) {
  2. this.length = length
  3. this.width = width
  4. Rectangle.prototype.getArea = function () {
  5. return this.length * this.width
  6. }
  7. }
  8. function Square(length) {
  9. Rectangle.call(this, length, length)
  10. }
  11. Square.prototype = Object.create(Rectangle.prototype, {
  12. constructor: {
  13. value: Square,
  14. enumerble: true,
  15. writeable: true,
  16. configurable: true
  17. }
  18. })
  19. var square = new Square(3)
  20. console.log(square.getArea());//9
  21. console.log(square instanceof Square);//true
  22. console.log(square instanceof Rectangle);//true

上面的代码通过构造函数和原型上面添加静态方法实现了 Rectangle 父类,然后子类 Square 通过 Rectangle.call(this,length,length) 调用了父类的构造函数,Object.create 会在内部创建一个空对象来连接两个原型对象,再手动将 constructor 指向自身。这种方法实现继承代码繁杂且不利用理解,于是ES6 class 类的创建让继承变得更加简单,使用extends 关键字来指定当前类所需要继承的父类,生成的类的原型会自动调整,还可以使用 super() 方法来访问基类的构造器。具体代码如下:

  1. class Rectangle {
  2. constructor(length, width) {
  3. this.length = length
  4. this.width = width
  5. }
  6. getArea() {
  7. return this.length * this.width
  8. }
  9. }
  10. class Square extends Rectangle {
  11. constructor(length) {
  12. super(length, length)
  13. }
  14. getArea() {
  15. return this.length + this.length
  16. }
  17.  
  18. }
  19. var square = new Square(3)
  20. console.log(square.getArea());//6
  21. console.log(square instanceof Square);//true
  22. console.log(square instanceof Rectangle);//true

上面的代码中 Square 类重写了基类的 getArea() 方法,当派生的子类中函数名和基类中函数同名的时候,派生类的方法会屏蔽基类的方法,同时也可以再子类中getArea () { return super.getArea() }中调用基类的方法进行扩展。

4.继承类的静态成员

静态成员:直接在构造器上添加的额外的方法。例如ES5中在原型上添加的方法就属于静态成员,ES6 class类引入简化了静态成员的创建,只需要在方法与访问器属性的名称前添加 static关键字即可。例如下面的代码用于区分静态方法和实例方法。

  1. function PersonType(name) {
  2. this.name = name;
  3. }
  4. // 静态方法
  5. PersonType.create = function(name) {
  6. return new PersonType(name);
  7. };
  8. // 实例方法
  9. PersonType.prototype.sayName = function() {
  10. console.log(this.name);
  11. };  var person = PersonType.create("Nicholas");

在ES6中要想使用静态成员如下:

  1. class Rectangle {
  2. constructor(length ,width) {
  3. this.length = length
  4. this.width = width
  5. }
  6. getArea() {
  7. return this.length * this.width
  8. }
  9. static create(length,width) {
  10. return new Rectangle(length , width)
  11. }
  12. }
  13. class Square extends Rectangle{
  14. constructor (length){
  15. super(length,length)
  16. }
  17. }
  18. var square =Square.create(3,4)
  19. console.log(square.getArea());//12
  20. console.log(square instanceof Square);//false
  21. console.log(square instanceof Rectangle);//true

上面的代码中,一个新的静态方法 create() 被添加到 Rectangle 类中,通过继承,以Square.create() 的形式存在,并且其行为方式与Rectangle.create() 一样。需要注意静态成员不懂通过实例来访问,始终需要直接调用类自身来访问他们。

写在最后

以上就是本文的全部内容,希望给读者带来些许的帮助和进步,方便的话点个关注,小白的成长踩坑之路会持续更新一些工作中常见的问题和技术点。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号