// ES5 实现
Function.prototype.myCall = function (ctx) {
// ctx = ctx || window;
ctx = ctx || globalThis; // node环境
var fn = "00" + Math.random();
while (ctx.hasOwnProperty(fn)) {
fn = "00" + Math.random();
}
ctx[fn] = this;
var res;
if (arguments.length < 2) {
res = ctx[fn]();
} else {
var fnArgs = [];
for (let i = 1; i < arguments.length; i++) {
fnArgs.push("arguments[" + i + "]");
}
res = eval("ctx[fn](" + fnArgs + ")");
}
delete ctx[fn];
return res;
};
globalThis.name = "Joe";
let Person = {
name: "James",
sayName() {
console.log(this.name, arguments);
},
};
let Person1 = {
name: "Kobe",
};
Person.sayName();
Person.sayName.myCall();
Person.sayName.myCall(Person1, 30, "God");
上一篇
JavaScript 手写 bind 方法.md
Function.prototype.myBind = function () {
// 返回一个绑定this的函数,我们需要在此保存this
let thatFn = this;
if (typeof thatFn !== "
2020-11-13
下一篇
排序
冒泡排序function bubbleSort(list) {
for (let i = 0; i < list.length; i++) {
let isSorted = true;
for (let j = 0
2020-10-15