d-overload

1.0.1 • Public • Published

js 函数重载

js 函数重载

使用方式

安装

npm i d-deep-clone

函数解释

createOverload

创建一个被重载的函数

const overload = createOverload();

overload.addImplement

添加函数实现,前面参数为被重载函数类型,最后为函数实现,函数实现是必要参数,不可忽略

// 添加实现函数
overload.addImplement(() => {
  console.log(1);
});

overload.addImplement("string", (str) => {
  console.log(typeof str);
});

overload.addImplement("number", (num) => {
  console.log(typeof num);
});

overload.addImplement("boolean", (bool) => {
  console.log(typeof bool);
});

overload.addImplement("object", "string", "number", (obj, str, num) => {
  console.log(typeof obj, str, num);
});

使用样例

import createOverload from "d-overload";

const overload = createOverload();

// 添加实现函数
overload.addImplement(() => {
  console.log(1);
});

overload.addImplement("string", (str) => {
  console.log(typeof str);
});

overload.addImplement("number", (num) => {
  console.log(typeof num);
});

overload.addImplement("boolean", (bool) => {
  console.log(typeof bool);
});

overload.addImplement("object", "string", "number", (obj, str, num) => {
  console.log(typeof obj, str, num);
});

overload();
overload("string");
overload(2);
overload(false);
overload({ a: 1, b: 2 }, "xxx", 333);

Package Sidebar

Install

npm i d-overload

Weekly Downloads

2

Version

1.0.1

License

ISC

Unpacked Size

4.39 kB

Total Files

4

Last publish

Collaborators

  • ducaoya