What is the usage of bind in javascript

藏色散人
Release: 2023-01-07 11:47:13
Original
7184 people have browsed it

The bind method in JavaScript is used to create a new function. Its usage syntax is "function.bind(thisArg[, arg1[, arg2[, ...]]])", where the parameter thisArg represents the call The value passed to the target function as the this parameter when binding the function.

What is the usage of bind in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is the usage of bind in javascript?

Function.prototype.bind()

bind() method creation For a new function, when bind() is called, this of the new function is specified as the first parameter of bind(), and the remaining parameters will be used as parameters of the new function for use when calling.

Syntax

function.bind(thisArg[, arg1[, arg2[, ...]]])
Copy after login

Parameter thisArg:

The value passed to the target function as the this parameter when calling the bound function. If the new operator is used to construct the bound function, this value is ignored. When using bind to create a function in setTimeout (provided as a callback), any primitive value passed as thisArg will be converted to object. If the argument list of the bind function is empty, or thisArg is null or undefined, the execution scope's this will be treated as the new function's thisArg.

Parameters arg1, arg2, ...:

Parameters that are preset into the parameter list of the bound function when the target function is called.

Return value:

Returns a copy of the original function with the specified this value and initial parameters.

Description:

bind() function will create a new binding function (bound function, BF). A bound function is an exotic function object (an ECMAScript 2015 term) that wraps the original function object. Calling a bound function usually causes the wrapper function to be executed.

Bound functions have the following internal properties:

[[BoundTargetFunction]] - the wrapped function object

[[BoundThis]] - when the wrapped function is called The value always passed as the this value.

[[BoundArguments]] - List, any call to the wrapper function will first fill the argument list with list elements.

[[Call]] - Execute the code associated with this object. Called via a function call expression. The parameters of the inner method are a this value and a list containing the parameters passed to the function through the calling expression.

When a bound function is called, it calls the internal method [[Call]] on [[BoundTargetFunction]], like this Call(boundThis, args). where boundThis is [[BoundThis]] and args is [[BoundArguments]] plus the argument list passed in through the function call.

Bound functions can also be constructed using the new operator, which will behave as if the target function has been constructed. The supplied this value is ignored, but the forward parameters are still provided to the mock function.

Recommended study: "javascript basic tutorial"

The above is the detailed content of What is the usage of bind in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!