@bluelovers/extend-bases

中文文档
2.0.2发布于 2022/08/31MITTypeScriptESM + CJS近 30 天下载 3413

A simple library for multiple inheritance in JavaScript.

原始使用文档
文档目录6 节
  1. Installation
  2. Getting started
  3. For more details
  4. Implementation details
  5. TypeScript support
  6. Contributing

extend-bases: A simple library for multiple inheritance in JavaScript.

This library can be used to achieve multiple inheritance in JavaScript, but it sets itself apart from other libraries for multiple inheritance because it isolates the base classes and prevents the properties and methods from colliding, while still allowing easy access to the properties and methods of the base classes.

Installation

Simply run:

npm add extend-bases

Or:

yarn add extend-bases

To import:

import {bases, defineProperties, isInstanceOf} from "extend-bases";

Or:

const {bases, defineProperties, isInstanceOf} = require("extend-bases");

Getting started

To inherit from multiple bases simply extend from bases(Class1, Class2, ...). In the constructor pass the instances of the classes to the super constructor.

Then you can access the base properties and methods directly from this., the first class with that property or method will be given precedence.

To access a particular base use this.bases[index].

Replace all instances of v instanceof Class with isInstanceOf(v, Class), it’s just that simple.

For more details

  • Read the Handy manual, it’s very simple & small and gives enough information to get started.
  • Then read the Documentation. it’s not too big and easy to go through.
  • Then, if you have enough time or are interested, read the tests and source code of the project.

Implementation details

This library uses Proxies, so sadly no IE11 support, but otherwise this library also imports es-shims for Reflect.ownKeys(ES6 but used very often and an es-shim exists for it) and Array.prototype.flatMap(ES2019, very new), which were found to be the bottlenecks, for some browsers.

This library uses proxies for easy access in the class instances, and it merges the prototypes of the base classes, but for isolation of base classes it creates a new function wrapping the method call. It’s just best to look at the source code for this.

TypeScript support

The library is by default written in TypeScript, which you all should be using if you aren’t, for great code completion. The details of the TypeScript definitions are a bit fiddly, so it’s best not to worry about them, they work, or you can just look at the source code.

Contributing

Feel free to open a pull request, fix bugs, or read the source code. It’s all up to you.

版本与发布记录

@bluelovers/extend-bases 共发布 3 个版本,首次发布于 2021/08/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.2latest2022/08/314 年前
2.0.12022/08/314 年前
1.0.32021/08/165 年前

关于 @bluelovers/extend-bases 的常见问题

@bluelovers/extend-bases 是什么?

A simple library for multiple inheritance in JavaScript.

如何安装 @bluelovers/extend-bases?

在项目目录运行 npm install @bluelovers/extend-bases,或使用 pnpm add / yarn add / bun add。

@bluelovers/extend-bases 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bluelovers/extend-bases 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bluelovers/extend-bases 可以商用吗?

@bluelovers/extend-bases 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bluelovers/extend-bases 的下载量有多少?

根据 npm 官方下载统计,@bluelovers/extend-bases 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,413 次,日均约 114 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bluelovers/extend-bases 还在维护吗?

最新版本 2.0.2 发布于 2022/08/31(4 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/08/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。