js手写一个call
js手写一个call
/**
* 手写一个call方法
* 函数名字为myCall
* symbol优化
*/
let obj = {
name: '白衣',
age: 3,
}
function getYaer (name, age) {
// console.log('函数内的this',this);
let nextYear = `${name},明年${age + 1}岁`
return nextYear
}
Function.prototype.myCall = function (targetObj, ...args) {
// symbol优化
let key = Symbol('key')
targetObj[key] = this
let resus = targetObj[key](...args)
delete targetObj[key]
return resus
}
getYaer.myCall(obj, 'joker', 3)
本文来自博客园,作者:jialiangzai,转载请注明原文链接:https://www.cnblogs.com/zsnhweb/articles/17764306.html

浙公网安备 33010602011771号