A simple function binding
Function binding is often used when JavaScript interacts with DOM. Define a function and then bind it to an event trigger of a specific DOM element or collection. The binding function is often combined with a callback function Used with event handlers to pass functions as variables while preserving the code execution environment.
function bind(fn,context){
. }
onclick=bind(handler.handlerFun,handler); The function can be bound to the specified environment through the custom bind function. The bind() function receives two parameters: a binding function, an execution environment, and returns an execution environment A function that calls the bound function. It looks simple, but its function is very powerful. A closure is created in bing(). The closure uses apply() to call the passed function, and passes the execution environment and parameters to apply(). The arguments here are Internal anonymous function, not bind(). When the returned function is called, it executes the passed function in the given function, given all arguments. In the above example, calling handler.handlerFun can still get the parameter event, because all parameters are passed to it through the bound function.
Summary