ES5实现原生/ES6方法
实现 bind apply call 方法
实现call
Function.prototype.myCall = function (context, ...args) {
context = context || window;
// 这里使用fn可能存在context已有对应属性,不严谨,可以使用Symbol对象生成唯一属性key
context.fn = this;
const res = context.fn(...args);
delete context.fn;
return res;
}实现apply
Function.prototype.myApply = function (context, args) {
context = context || global;
// 这里使用fn可能存在context已有对应属性,不严谨,可以使用Symbol对象生成唯一属性key
context.fn = this;
const res = context.fn(...args);
delete context.fn;
return res;
}实现bind
instanceOf 实现
Json.stringify Json.parse实现
Last updated