甲板

这是叉子 开发的甲板 将错误名称的"发号施令"重命名为"发号施令",并添加了实际"发号施令".
简洁地执行《行动纲领》 3个 4个 最有用的 装饰器数字 :
@bind: 使数值为this方法内的常数@throttle:节流阀调用方法@debounce: 调用方法@memoize: 基于参数的缓存返回值
装饰者通过用宣示性说明来取代常见图案的噪声来帮助简化代码.
相反地,装饰者也可能被过度地使用并产生出模糊.
Decko 建立 3个 页:1 立即可识别的标准装饰器,因此可以避免在自己的代码库中创建装饰器.
💡 提示 : 甲板特别适合 预选类组件。 。 。 .
💫 说明:
- 对于 Babel 6+, 请确定安装 bail-plugin- transform-decororers-legacy 变形人-遗产。 。 。 .
- 对于打字符, 请确定启用
{"experimentalDecorators": true}在你的tsconfig.json中.
安装
备查 无数字 :
npm i -S @agentepsilon/decko
使用量
每一种装饰方法都可以作为命名的导入.
import { bind, memoize, throttle, debounce } from '@agentepsilon/decko';
@bind
class Example {
@bind
foo() {
// the value of `this` is always the object from which foo() was referenced.
return this;
}
}
let e = new Example();
assert.equal(e.foo.call(null), e);
@memoize
从装饰函数返回缓存值 . 使用第一个参数作为缓存密钥 . 缓存密钥总是转换为字符串 .
选项 :
caseSensitive: false- 说吧 使缓存密钥对大小写不敏感
cache: {}- 说吧 用于种子或共享条目的预供给缓存存储
class Example {
@memoize
expensive(key) {
let start = Date.now();
while (Date.now()-start < 500) key++;
return key;
}
}
let e = new Example();
// this takes 500ms
let one = e.expensive(1);
// this takes 0ms
let two = e.expensive(1);
// this takes 500ms
let three = e.expensive(2);
@throttle
喉管调用到装饰功能. 节流阀的意思是"最多每N毫秒调用一次". 所有外向函数调用都被整理成单一的内向调用,只有传递到被调用函数的最新(最近)参数.
选项 :
delay: 0- 说吧 需要缓冲的毫秒数 .
class Example {
@throttle
foo() {
return this;
}
}
let e = new Example();
// this will only call foo() once:
for (let i=1000; i--) e.foo();
@debounce
取消调用装饰功能 。 发音的意思是"只有在它没有被调用N ms后才能调用". 所有外向函数都发出清除当前已排队的呼叫,然后排出新的呼叫,有效地将呼叫进一步延迟. 一样
@throttle,只有最近的论点被传递.选项 :
delay: 0- 说吧 等待重复呼吁的毫秒数 .
class Example {
@debounce
foo() {
return this;
}
}
``
---
License
-------
MIT