Home > Web Front-end > JS Tutorial > body text

What does bind mean in javascript

藏色散人
Release: 2023-01-05 16:11:07
Original
5566 people have browsed it

bind in javascript is a new method in EcmaScript5. The bind() method is used to create a new function. When called, set its this keyword to the provided value. When calling the new function, Provide a given sequence of arguments before any provision.

What does bind mean in javascript

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

A brief talk about bind in js

This article mainly introduces the bind application in js. The article introduces it in detail through sample code. It will be helpful for everyone's learning or The work has a certain reference and learning value. Friends who need it can follow the editor to learn and learn together.

When it comes to the bind method, I guess everyone will also think of the call method and the apply method; they are all within the Function object. The first parameter of the built methods is used to change the pointer of this in the calling method. It should be noted that bind returns a new function so that it can be called later; apply and call call the original function immediately. Today we mainly explain the understanding and use of the bind method.

The bind method is a new method in EcmaScript5. This method is introduced on mdn as follows:

The bind() method creates a new function (called a binding function). When it is When called, set its this keyword to the supplied value, and when calling a new function, supply a given sequence of arguments before any supply.

Syntax:

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

The parameter thisArg means: when the bound function is called, this parameter will be used as the pointer to this. This parameter has no effect when calling the bound function using the new operator.

The parameters arg1, arg2, ... mean: when the bound function is called, these parameters will be passed to the bound method before the actual parameters.

Let’s look at an example first:

this.name="jack";
var demo={
name:"rose",
getName:function(){return this.name;}
}
 
console.log(demo.getName());//输出rose 这里的this指向demo
 
var another=demo.getName;
console.log(another())//输出jack 这里的this指向全局对象
  
//运用bind方法更改this指向
var another2=another.bind(demo);
console.log(another2());//输出rose 这里this指向了demo对象了
Copy after login

bind application

You can preset initial parameters for a function:

function a(){
return Array.prototype.slice.call(arguments);//将类数组转换成真正的数组
}
var b=a.bind(this,15,20)
alert(b());//弹出 15,20
var s=b(25,30);
alert(s);//弹出 15,20,25,30
Copy after login

js bind multiple times It is only valid for the first time

var getname = function(){console.log(this.name)};
var m = getname.bind({name:'q1'}).bind({name:'q2'});
m();
Copy after login

The output is q1

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What does bind mean 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 [email protected]
Popular Tutorials
More>
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!