Call function implementation
Function.prototype.ltCall = function(thisArg, ... arr) {
var result = undefined;
var fn = this;
// Determine if thisArg passed is null or undefind
thisArg = thisArg ? Object(thisArg) : window;
// define a symbol variable to prevent thisArg from having the same attribute name
var symbol = Symbol(a);// Add a symbol attribute to thisArg
thisArg[symbol] = fn;
// Check whether arR is null
arr = arr ? arr : [];
// Get the symbol attribute on the object
var symbols = Object.getOwnPropertySymbols(thisArg);
for (key of symbols) {
// Point this to thisArg via an implicit callresult = thisArg[key](... arr);// When the function is called, delete the fn attribute
delete thisArg[key];
}
return result;
};
Copy the code
Apply function implementation
Function.prototype.ltapply = function(thisArg, arr) {
var result = undefined;
var fn = this;
thisArg = thisArg ? Object(thisArg) : window;
thisArg.fn = fn;
// define a symbol variable to prevent thisArg from having the same attribute name
var symbol = Symbol(a);// Add a symbol attribute to thisArg
thisArg[symbol] = fn;
// Check whether arR is null
arr = arr ? arr : [];
// Get the symbol attribute on the object
var symbols = Object.getOwnPropertySymbols(thisArg);
for (key of symbols) {
// Point this to thisArg via an implicit callresult = thisArg[key](... arr);// When the function is called, delete the fn attribute
delete thisArg[key];
}
return result;
};
Copy the code
Bind function implementation
Function.prototype.ltbind = function(thisArg, ... arr) {
var fn = this;
thisArg = thisArg ? Object(thisArg) : window;
// define a symbol variable to prevent thisArg from having the same attribute name
var symbol = Symbol(a);// Add a symbol attribute to thisArg
thisArg[symbol] = fn;
arr = arr ? arr : [];
function proxyFn() {
var result = undefined;
ThisArg gets all symbol attributes on thisArg
var symbols = Object.getOwnPropertySymbols(thisArg);
for (key of symbols) {
// Point this to thisArg via an implicit callresult = thisArg[key](... arr);// When the function is called, delete the fn attribute
delete thisArg[key];
returnresult; }}return proxyFn;
};
Copy the code
- Thank you very muchWang hong yuanThe teacher’sDeep dive into JavaScript advanced syntaxLet me learn a lot
JavaScript
Knowledge.