一次函数
2024年03月12日
一、认识
二、实现
- JavaScript
- TavaScript
function once(fn) {
var called = false;
return function () {
if (!called) {
called = true;
fn.apply(this, arguments);
}
};
}
var pay = once(function (money) {
console.log(`支付了 ${ money } RMB`);
});
pay(5);
pay(10);
function once<T extends (...args: any[]) => any>(fn:T):T{
let called = false;
return function(){
if(!called){
called = true;
fn.apply(this,arguments as any);
}
} as any
}
const pay = once(function(money){
console.log(`支付了 ${ money } RMB`);
});
pay(5);
pay(10)