You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
/** |
|
* @description 模拟 apply&call |
|
* @date 2022-07-01 |
|
* @article https://zhuanlan.zhihu.com/p/51259309 |
|
*/ |
|
|
|
function getGlobalObject() { |
|
return this; |
|
} |
|
|
|
(Function as any).prototype.myApply = function apply( |
|
thisArg = getGlobalObject(), |
|
argsArray = [] |
|
) { |
|
if (typeof this !== "function") { |
|
throw new TypeError(this + " is not a function"); |
|
} |
|
|
|
if (argsArray !== new Object(argsArray)) { |
|
throw new TypeError("CreateListFromArrayLike called on non-object"); |
|
} |
|
|
|
if (thisArg == null) thisArg = getGlobalObject(); |
|
if (argsArray == null) argsArray = []; |
|
|
|
thisArg = new Object(thisArg); |
|
thisArg["__fn"] = this; |
|
const result = thisArg["__fn"](...argsArray); |
|
delete thisArg["__fn"]; |
|
|
|
return result; |
|
}; |
|
|
|
// 额外情况 |
|
// 1. 如果 thisArg 中已经存在一个同名 __fn 属性呢? |
|
// 2. 解决以上问题不允许使用 Sybmol |
|
|
|
(Function as any).prototype.myCall = function call(thisArg) { |
|
const argsArray: any[] = []; |
|
for (let index = 1; index <= arguments.length - 1; index++) { |
|
argsArray[index - 1] = arguments[index]; |
|
} |
|
|
|
return this.myApply(thisArg, argsArray); |
|
}; |
|
|
|
const testObj = { |
|
name: "Hello", |
|
child: 200, |
|
index: 0, |
|
}; |
|
|
|
function tsetFunction(...args) { |
|
console.log("value", args); |
|
console.log("name", this.name); |
|
console.log("child", this.child); |
|
console.log("index", this.index); |
|
} |
|
|
|
(tsetFunction as any).myCall(testObj, 100, 200, 300, 400, 500, 600, 700);
|
|
|