一、call 方法
call
,语法为 call(fn, obj, ...args)
,功能为执行 fn
,使 this
为 obj
,并将后面的 n
个参数传给 fn
,等同于函数对象的 call
方法。call
的实现,代码如下所示:
export function call (Fn, obj, ...args) {
if (obj === undefined || obj === null) {
obj = globalThis;
}
obj.temp = Fn;
let result = obj.temp(...args);
delete obj.temp;
return result;
}
- 手写函数
call
的应用,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>call 方法</title>
<script src="../../src/function/call.js"></script>
</head>
<body>
<script>
function add (a, b) {
console.log(this);
return a + b + this.c;
}
let obj = {
c: 100
}
window.c = 200;
console.log(call(add, obj, 10, 20));
console.log(call(add, null, 30, 40));
</script>
</body>
</html>
二、apply 方法
apply
,语法为 apply(fn, obj, args)
,功能为执行 fn
,使 this
为 obj
,并将 args
数组中的元素传给 fn
,等同于函数对象的 apply
方法。apply
的实现,代码如下所示:
export function apply (Fn, obj, args) {
if (obj === undefined || obj === null) {
obj = globalThis;
}
obj.temp = Fn;
let result = obj.temp(...args);
delete obj.temp;
return result;
}
- 手写函数
apply
的应用,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>apply 方法</title>
<script src="./../../src/function/apply.js"></script>
</head>
<body>
<script>
function add (a, b) {
console.log(this);
return a + b + this.c;
}
let obj = {
c: 521
};
window.c = 1314;
console.log(apply(add, obj, [10, 20]));
console.log(apply(add, null, [30, 40]));
console.log(obj);
</script>
</body>
</html>
三、bind 方法
bind
,语法为 bind(fn, obj, ...args)
,功能为 给 fn
绑定 this
为 obj
,并指定参数为后面的 n
个参数,等同于函数对象的 bind
方法。bind
的实现,代码如下所示:
import { call } from './call';
export function bind(Fn, obj, ...args) {
return function (...args2) {
return call(Fn, obj, ...args, ...args2);
}
}
- 手写函数
bind
的应用,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>bind 方法</title>
<script src="./../../src/function/call.js"></script>
<script src="./../../src/function/bind.js"></script>
</head>
<body>
<script>
function add(a, b) {
console.log(this);
console.log(arguments);
return a + b + this.c;
}
let obj = {
c: 521
}
window.c = 1314;
let fn4 = bind(add, obj, 10, 20);
console.log(fn4(30, 50));
</script>
</body>
</html>
四、节流方法
- 函数的节流,函数需要频繁触发时,函数执行一次后,只有大于设定的执行周期后才会执行第二次。适合多次事件按时间做平均分配触发。
throttle
,语法为 throttle(callback, wait)
,功能为创建一个节流函数,在 wait
毫秒内最多执行 callback
一次。throttle
的实现,代码如下所示:
export function throttle (callback, wait) {
let start = 0;
return function (e) {
let now = Date.now();
if (now - start >= wait) {
callback.call(this, e);
start = now;
}
}
}
- 手写函数
throttle
的应用,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>函数的节流 throttle</title>
<script src="../../src/function/throttle.js"></script>
<style>
body {
height: 2000px;
background: linear-gradient(#125, #eae);
}
</style>
</head>
<body>
<script>
window.addEventListener('scroll', throttle(function(e){
console.log(e);
}, 500));
</script>
</body>
</html>
五、防抖方法
debounce
,语法为 debounce(callback, wait)
,功能为创建一个防抖函数,该函数会从上一次被调用后,延迟 wait
毫秒后调用 callback
。debounce
的实现,代码如下所示:
export function debounce (callback, time) {
let timeId = null;
return function (e) {
if (timeId !== null) {
clearTimeout(timeId);
}
timeId = setTimeout(() => {
callback.call(this, e);
timeId = null;
}, time);
}
}
- 手写函数
debounce
的应用,代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>函数的防抖 debounce</title>
<script src="../../src/function/debounce.js"></script>
</head>
<body>
<script>
let input = document.querySelector('input');
input.onkeydown = debounce(function(e){
console.log(e.keyCode);
}, 1000);
</script>
</body>
</html>