Function.prototype.bind( boundThis, ...boundArgs )
Example
const person = {
getFullName: function () {
return this.firstName + " " + this.lastName;
}
}
const thisPerson = {
firstName : "John",
lastName : "Xu",
}
let getFullName = person.getFullName.bind(thisPerson);
console.log( getFullName() )
Function.prototype.bind( boundThis, ...boundArgs )
- Let targetFunction be the this value. // for our example, person.getFullName
- If IsCallable(targetFunction) is false, throw a TypeError exception.
- Let boundArgs be a new List consisting of all of the argument values provided after boundThis(for our example, empty) .
- Let F be ?BoundFunctionCreate(targetFunction, boundThis, boundArgs)
return {
[[BoundTargetFunction]]: targetFunction,
[[BoundThis]]: boundThis,
[[BoundArguments]]: boundArgs,
[[Prototype]]:targetFunction.[[Prototype]],
}
- Perform SetFunctionName(F, name = Get(Target, "name"), prefix = "bound").
No comments:
Post a Comment