ES6 Class & super All In One
1.如何使用 js 实现一个 ES6 中 class 的 extends 功能 All In One2.ECMAScript 2023 & Temporal All In One3.js ES5 arguments & arguments.callee & this & ES6 new.target All In One4.ES6 Map Object index All In One5.TypeScript Decorator Metadata All In One6.js ES6 Proxy All In One7.js ES6 Reflect All In One8.js @decorator All In One9.Vue warn All In One10.ES6 解构赋值 All In One11.ES6 & export & export default All In One12.ES6 Async Await bug All In One13.ES6 destructuring assignment & default value & rename All In One14.ES6 Symbol All In One15.ES6 Map & WeakMap All In One16.ES6 Reflect All In One17.ES6 Proxy & Reflect All In One18.composition-api & setup19.ES6 Arrow Function & this bug20.ES5 arguments vs ES6 ...rest params All In One21.ES6 export & export.default All In One22.es6 string html23.js create Array ways All In One24.ES6 version repeatify25.es6 curry function26.JavaScript convert ES6 Map to Array All In One27.JavaScript string repeat methods All In One28.ES6 Set All In One29.ES6 Map All In One30.ES6 Arrow Function All In One31.如何用 js 实现一个 ES6 class 类函数 All In One32.ES6 进制字面量 All In One33.redux 中间件 redux-saga 使用教程34.how to convert Map to Object in js35.ES6 Set vs ES5 Array36.ES6 getter & setter37.ES6 arrow function vs ES5 function All In One38.ES6 Class vs ES5 constructor function All In One39.ES6 Generator vs ES6 async/await40.ES6 Map vs ES5 Object All In One41.js swap array42.ES6 ...rest In Action43.Webpack 4.x 默认支持 ES6 语法44.2020 面经(大前端,算法,设计模式,架构)45.js ES5 inheritance All In One46.JavaScript Learning Paths(ES5/ES6/ES-Next)47.ES5 function & ES6 class & method type48.Unicode & \u2028 & \u2029 & ES6 Template literals All In One49.random array & shuffle 洗牌算法 / 随机算法50.js in depth: ES6 destructuring assignment51.gradient text & gradient background52.V8 & ECMAScript & ES-Next53.js 数据劫持 (应用场景) All In One54.ES6 Arrow Function return Object
55.ES6 Class & super All In One
56.ES6 & tagged-template-literals & template strings All In One57.Enums & JavasScript & TypeScript58.es6 & map & set59.ES6 & import * & import default & import JSON All In One60.vue @ path61.ES6 & Classes & Interface62.ES6 & class extends All In One63.Google Advanced Search operators All In One64.React Native & ES6 & emoji All In One65.ES6 & Map & hashMap66.array to object67.ES6 Set & Map68.ES6 Destructuring Assignment All In One69.vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug70.HTML Imports & polyfill71.yarn & vue-cli & hui-cli All In One72.ES6 Map & WeakMap & ES6 Set & WeakSet73.ES6 polyfills & String.includes() & Array.includes()74.ES6 Arrow Function & this bug75.ES6 Proxies76.ES6 import json77.disable VS Code auto format javascript when save codes78.how to create a javascript components framework using HTML5, CSS3, ES6 All In One79.eslint es6 syntax & object rest spread80.!function() & IIFE81.React.createClass vs. ES6 Class Components82.ES2015 (ES6) 新特性: 20 个ES6 Class & super
ES6 class extends
// 父类
class Human {
constructor (name) {
this.name = name ?? Human.name;
// this.name = name ?? Human.constructor.name;
}
}
// 子类继承父类
class Person extends Human {
constructor (name, age) {
super(name ?? Person.name);
// 在调用 this 前,必须先调用 super ✅
this.age = age;
}
static getClassName () {
console.log('Person.name =', Person.name);
return Person.name;
}
getName () {
console.log('this.name =', this.name);
return this.name;
}
getAge () {
console.log('this.age =', this.age);
return this.age;
}
}
const person = new Person('eric', 18);
// static method 静态方法
Person.getClassName();
// instance methods 实例方法
person.getName();
person.getAge();
// Person.name = Person
// this.name = eric
// this.age = 18
override 覆盖
abstract class Animal {
constructor() {}
// 抽象方法
abstract speak(): void;
// 代码复用,定义公用方法并实现
eat(food: string): void{
console.log("eat =", food);
}
}
class Dog extends Animal {
food: string = '';
// override 字类覆盖父类方法
constructor(food: string) {
super();
// super
super.eat(food);
}
override speak() {
console.log("wang!");
}
}
const puppy = new Dog('meat');
puppy.speak();
puppy.eat('🐶');
class categories extends Animal {
food: string = '';
// override 字类覆盖父类方法
constructor(food: string) {
super();
// this
this.eat(food);
}
override speak() {
console.log("wang!");
}
}
const kitty = new Dog('fish');
kitty.speak();
kitty.eat('😸');
"use strict";
class Animal {
constructor() { }
// 代码复用,定义公用方法并实现
eat(food) {
console.log("eat =", food);
}
}
class Dog extends Animal {
// override 字类覆盖父类方法
constructor(food) {
super();
this.food = '';
// super
super.eat(food);
}
speak() {
console.log("wang!");
}
}
const puppy = new Dog('meat');
puppy.speak();
puppy.eat('🐶');
class categories extends Animal {
// override 字类覆盖父类方法
constructor(food) {
super();
this.food = '';
// this
this.eat(food);
}
speak() {
console.log("wang!");
}
}
const kitty = new Dog('fish');
kitty.speak();
kitty.eat('😸');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super
ES6 Class name
class Human {
constructor (name) {
this.name = name ?? Human.name;
// this.name = name ?? Human.constructor.name;
}
}
class Person extends Human {
constructor (name) {
super(name, alias);
this.alias = alias ?? Person.name;
// this.alias = alias ?? Person.constructor.name;
}
}
https://bobbyhadz.com/blog/javascript-get-class-name-of-object
Typescript
interface Animal {
speak(): void;
}
class Dog implements Animal {
food: string = '';
// override 字类覆盖父类方法
constructor(food: string) {
// super();
this.food = food;
}
speak() {
console.log("wang!");
}
eat(food: string) {
// ?? => !== null && !== void 0 ? :
// void 0 === undefined
console.log(food ?? this.food);
}
}
const puppy = new Dog('meat');
puppy.speak();
puppy.eat('🐶');
"use strict";
class Dog {
// override 字类覆盖父类方法
constructor(food) {
this.food = '';
// super();
this.food = food;
}
speak() {
console.log("wang!");
}
eat(food) {
// ?? => !== null && !== void 0 ? :
// void 0 === undefined
console.log(food !== null && food !== void 0 ? food : this.food);
}
}
const puppy = new Dog('meat');
puppy.speak();
puppy.eat('🐶');
https://www.typescriptlang.org/play?ssl=24&ssc=1&pln=1&pc=1#
EventEmitter
// 5.实现一个 EventEmitter类,这个类包含以下方法:
// on(监听事件,该事件可以被触发多次)
// once(也是监听事件,但只能被触发一次)
// fire(触发指定的事件)
// off(移除指定事件的某个回调方法或者所有回调方法)
class EventEmitter {
constructor() {
this.events = {};
}
on(name, callback) {
this.events[name] = {
callback,
once: false,
};
}
once(name, callback) {
this.events[name] = {
callback,
once: true,
};
}
off(name) {
let keys = Object.keys(this.events);
if (keys.includes(name)) {
delete this.events[name];
}
}
fire(name, person) {
// dispatchEvent
let keys = Object.keys(this.events);
if (keys.includes(name)) {
let fun = this.events[name].callback;
let once = this.events[name].once;
fun(person);
if (once) {
delete this.events[name];
}
}
}
}
const event = new EventEmitter()
event.on("drink", (person) => {
log(person + "喝水");
});
event.on("eat", (person) => {
log(person + "吃东西");
});
event.once("buy", (person) => {
log(person + "买东西");
});
event.fire("drink", "我");
// 我喝水
event.fire("drink", "我");
// 我喝水
event.fire("eat", "其它人");
// 其它人吃东西
event.fire("eat", "其它人");
// 其它人吃东西
event.fire("buy", "其它人");
//其它人买东西
event.fire("buy", "其它人");
//这里不会再次触发buy事件,因为once只能触发一次
event.off("eat") //移除eat事件
event.fire("eat", "其它人");
//这里不会触发eat事件,因为已经移除了
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11391270.html
未经授权禁止转载,违者必究!
合集:
ES6
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2018-08-21 blogs & cnblogs All In One
2018-08-21 GitHub GraphQL API All In One
2018-08-21 webpack 3.x best practical
2018-08-21 X-Frame-Options & iframe & CORS
2018-08-21 VSCode custom code snippets All In One
2016-08-21 CSS3 error
2016-08-21 web颜色名 速查列表,十六进制值,RGBA(),RGB() ,