1. 各种排序+查找算法
冒泡排序 快速排序 归并排序 插入排序 选择排序 顺序查找 二分查找 [https://blog.csdn.net/Sabrina_cc/article/details/106857519](()
2. call bind apply
apply 、 call 、bind 三者都是用来改变函数的this对象的指向的;第一个参数都是this要指向的对象,也就是想指定的上下文;
call() 方法在使用一个指定的 this 值和若干个指定的参数值的前提下调用某个函数或方法。
所以我们模拟的步骤可以分为:(1)将函数设为对象的属性(2)执行该函数(3)删除该函数
Function.prototype.myCall = function(context) {
/*if (typeof this !== ‘function’) {
throw new TypeError(‘Error’)
}*/
context = context || window; //第一个参数为null或者undefined时,this指向全局对象window
context.fn = this; // 非常关键:改变 this 的作用域
const args = […arguments].slice(1); //从 Arguments 对象中取取第二到最后一个参数放到一个数组里
const result = context.fn(…args); //把这个参数数组放到要执行的函数的参数里面去
delete context.fn; // 必须删除,会给 context 增加方法 fn
return result;
}
// 简单测试
let obj = {
name: ‘xxx’
};
function test(){
console.log(‘arguments=’,arguments); // Arguments(2) [1, 2, callee: ƒ, Symbol(Symbol.iterator): ƒ]
console.log(‘this=’,this); // {name: “xxx”, fn: ƒ}
}
test.myCall(obj,1,2);
apply 接受一个数组(或者类数组对象)作为参数输入
Function.prototype.myApply = function(context) {
/* if (typeof this !== ‘function’) {
throw new TypeError(‘Error’)
}*/
context = context || window;
context.fn = this;
let result;
if (arguments[1]) {
result = context.fn(…arguments[1]); // 处理参数和 call 有区别
} els