JavaScript中es6中的class和class的继承(一)

介绍构造类Class

class是其他MVC模式下语言常见的一种面向对象的封装 ,es6的class是与它们类似,拥有继承 ,封装 ,类似于js中的混合构造函数模式的原形模式的 面向对象的编程

基本写法

class Hello {
    constructor() {
        console.log('constructor');
        this.toString();
        this.toNum();
        this.toValue();
    }
    toString() {
        console.log('toString');
    }
}
//Hello类
//使用对象合并的方式 去外部拓展Class
Object.assign(Hello.prototype, {
    toValue() {
        console.log('toValue');
    },
    toNum() {
        console.log('toNum');
    }
});
const hello = new Hello();  //class表达式

** 输出结果 ** 这里必须要使用new 才能执行自带的constructor
在这里插入图片描述

什么是混合构造函数模式的原形模式

不喜欢废话 直接上案例

var AudioPlayer = function () {
    this.oAllMusicList = [];  //执行
    this.init();  //整个构造函数的初始化,调用这个方法的 以下不会执行
    console.log(1)  //不执行
};
AudioPlayer.prototype = {
    init() {
        this.getMusicItem(); // 随便的两个方法
        this.AudioCouldPlayed(); //
    },
	getMusicItem(){
		
	},
	AudioCouldPlayed(){
	
	}

最后

本人一旦更新就会更新 这个最后的链接

猜你喜欢

转载自blog.csdn.net/Aijn_faluts/article/details/88657780