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

第211题(2020-05-12):原型,class B 继承 class A 翻译成 es5 应该是什么样子?(酷家乐) #213

Open
qappleh opened this issue May 12, 2020 · 1 comment

Comments

@qappleh
Copy link
Owner

qappleh commented May 12, 2020

No description provided.

@qappleh
Copy link
Owner Author

qappleh commented Jun 22, 2020

JS继承方式有很多,主要分ES5和ES6继承的实现
先说一下ES5是如何实现继承的
ES5实现继承主要是基于prototype来实现的,具体有三种方式

一是原型链继承:即B.prototype-new A()

二是借用构造函数(call 或者 apply 的方式继承)

function B(name,age) {
         A.call(this,name,age)
  }

三是组合继承

组合继承是结合第一种和第二种方式
再说一下ES6是如何实现继承的
ES6继承是目前比较新,并且主流的集成方式,用class定义类,用extends继承类,用super()表示父类
例如:
创建A类

class A{
       constructor(){
             //构造器代码,new时自动执行
       }
       方法一(){//A类的方法}
       方法二(){//A类的方法}
}

创建B类并继承A类

class B extends A{
         constructor(){
          super()//表示父类
       }
}

实例化B类

 var b1=new B()
      b1.方法一()

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