/*
允许使用es6
*/
// Function.prototype.call = function (object) {
// let fun_name = Symbol()
// object.fun_name = this
// let arr = Array.from(arguments).slice(1)
// let value = object.fun_name(...arr)
// delete object.fun_name
// return value
// }
/*
不允许允许使用es6
*/
Function.prototype.call = function (object) {
//手动封装symbol
function mySymbol() {
let fun_name = Math.random() + '' + Date.now()
if (object.fun_name) {
return mySymbol()
}
return fun_name
}
let fun_name = mySymbol()
object.fun_name = this
let arr = []
for (let i = 1; i < arguments.length; i++) {
arr.push(`arguments[${i}]`)
}
let value = eval('object.fun_name(' + arr.join(',') + ')')
delete object.fun_name
return value
}
function helloWorld(a, b, c) {
console.log(a, b, c)
console.log('helloWorld!', a + b + c)
return a + b + c
}
let obj = {}
helloWorld.call(obj, 1, 2, 3)
手写js的call
最新推荐文章于 2024-04-10 18:00:15 发布