當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript-面向对象 class 继承
生活随笔
收集整理的這篇文章主要介紹了
JavaScript-面向对象 class 继承
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class繼承
class 關鍵字是在ES6引入的
ES6之前的寫法:
function Student(name) {this.name = name } // 給Student新增一個方法 Student.prototype.hello = function () {alert('Hello') }ES6的寫法:
// 定義一個 學生的 類 class Student1{constructor(name) {this.name = name}hello(){alert('hello')} } let xiaoming = new Student1('xiaoming') console.log(xiaoming.name) xiaoming.hello()繼承:
class Student1{constructor(name) {this.name = name}hello(){alert('hello')} } class xiaoStudent extends Student1{constructor(name, grade) {super(name);this.grade = grade}myGrade(){alert("我是一名小學生")} } let xiaoStu = new xiaoStudent('xiaoming1', 2) console.log(xiaoStu.name) // xiaoming1 console.log(xiaoStu.grade) // 2 console.log(xiaoStu) xiaoStu.myGrade()class的寫法和之前的寫法本質是一樣的,只是寫法不同
原型鏈:
https://www.cnblogs.com/loveyaxin/p/11151586.html
https://www.bilibili.com/video/BV1JJ41177di?p=18
總結
以上是生活随笔為你收集整理的JavaScript-面向对象 class 继承的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux-centos7 常用的基本命
- 下一篇: 前端的葵花宝典 - 红宝书《JavaSc