Class 助力 js 更加面向对象了
传统对象 - function构造函数
1function Course(teacher, course) {2 this.teacher = teacher;3 this.course = course;4}5Course.prototype.getCourse = function () {6 return `teacher: ${this.teacher}, course: ${this.course}`;7};89const course = new Course("YY", "ES6");
ES6
1class Course {2 constructor(teacher, course) {3 this.teacher = teacher;4 this.course = course;5 }6 getCourse() {7 return `teacher: ${this.teacher}, course: ${this.course}`;8 }9}10const course = new Course("YY", "ES6");
FAQ
本质?
语法糖
class 是什么类型?
1console.log(typeof Course); //function
class 是否有 prototype?
1console.log(Course.prototype); //{constructor: ƒ, getCourse: ƒ}
class 可以使用对象方法&属性么
1console.log(course.hasOwnProperty("teacher")); //true
属性定义?
两种定义属性的方式: 构造器 & 顶层定义
1class Course {2 constructor(teacher, course) {3 this._teacher = teacher;4 this.course = course;5 }6 getCourse() {7 return `teacher: ${this._teacher}, course: ${this.course}`;8 }9 get teacher() {10 return this._teacher;11 }12 set teacher(val) {13 this._teacher = val;14 }15}16const course = new Course('YY', 'ES6');1718// 意义何在?19// 1. 建立只读变量, *js如何建立只读变量20class Course1 {21 constructor(teacher, course) {22 this._teacher = teacher;23 this.course = course;24 }25 getCourse() {26 return `teacher: ${this._teacher}, course: ${this.course}`;27 }28 get teacher() {29 return this._teacher;30 }31}32const course1 = new Course1('YY', 'ES6');3334//修改只读变量会报错么?35course1.teacher = '222'; // - 无法改变,但不会报错363738// 2. *js中如何实现一个私有属性 - 闭包39class Course2 {40 constructor(teacher, course) {41 this._teacher = teacher;42 // 在constructor作用域中定义局部变量,内部通过闭包的形式对外暴露该变量43 let _course = 'es6';4445 this.getCourse = () => {46 return _course;47 }48 }49}5051class Course3 {52 #course = 'es6';53 constructor(teacher, course) {54 this._teacher = teacher;55 }56 }57 get course() {58 return `${#course}~`;59 }60 set course(val) {61 if (val) {62 this.#course = val;63 }64 }65}6667// 3. 封装核心 - 适配器模式68// 底层封装好通用core服务69class Utils {70 constructor(core) {71 this._main = core;72 this._name = 'myName';73 }74 get name() {75 ...this._main.name,76 name: ${this._name}77 }78 set name(val) {79 this._name = val;80 }81}
静态方法
直接挂载,无需实例化即可获取
1// ES52function Course(teacher, course) {3 this._teacher = teacher;4 this.course = course;5}6Course.call = function () {7 console.log("calling");8};910// ES611class Course {12 constructor(teacher, course) {13 this._teacher = teacher;14 }15 static call() {16 console.log("calling");17 }18}
继承
ES5 继承
1// 父构造函数2function Course(teacher, course) {3 this._teacher = teacher;4 this.course = course;5}6Course.call = function () {7 console.log("calling");8};9Course.prototype.send = function () {10 console.log("sending");11};12// 子构造函数13function Child() {14 // 初始化父类15 Course.call(this, "xx", "ES6");16 this.start = function () {17 console.log("starting");18 };19}20Child.prototype = Course.prototype;
ES6 继承
1// 父类2class Course {3 constructor(teacher, course) {4 this._teacher = teacher;5 this.course = course;6 }7 send() {8 console.log("sending");9 }10 static call() {11 console.log("calling");12 }13}1415// 子类16class Child extends Course {17 constructor() {18 super("xx", "ES6");19 }20 start() {21 console.log("starting");22 }23}