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>Document</title>
</head>
<body>
<script>
// 实现函数 callIt,调用之后满足如下条件
// 1、返回的结果为调用 fn 之后的结果
// 2、fn 的调用参数为 callIt 的第一个参数之后的全部参数
function callIt(fn) {
// [].slice.call(arguments, 1)就是裁切arguments传入的参数,保存起来操作;
return fn.apply(this, [].slice.call(arguments, 1));
}
function result() {
var a = 1;
b = 2;
var c = 3;
var test = function(first, second, third) {
return first === a && second === b && third === c;
};
return callIt(test, a, b, c);
}
var res = result();
console.log(res);
</script>
</body>
</html>
运行结果: