You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const isFunc = v => typeof v === 'function'
Function.prototype.customCall = function(context) {
if(!isFunc(this)) {
return undefined
}
/** ensure fn is a unique property in context */
const fn = Symbol()
/** mount this 'fn' property to context object */
context = context || globalThis
context[fn] = this
/** splice arguments */
const args = [...arguments].slice(1)
/** call this function */
const res = context[fn](...args)
/** delete this property in this context object */
delete context[fn]
return res
}
Function.prototype.customBind = function(context) {
if(!isFunc(this)) {
return undefined
}
const fn = this
const args = [...arguments].slice(1)
/** 'bind' function need return a function */
/** and simultaneity, you can change this context object become a 'Fn''s instance */
/** beside args, you can pass other params for call this function */
return function Fn() {
return fn.apply(
this instanceof Fn ? this : context,
args.concat(arguments)
)
}
}
The text was updated successfully, but these errors were encountered:
1. call:
2.
apply
:3.
bind
:所以可以这样写
The text was updated successfully, but these errors were encountered: