Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use bind in js

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use bind in js", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how to use bind in js".

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

The bind() method creates a new function (called a bound function) that, when called, sets its this keyword to the value provided, and provides a given sequence of arguments before any provision when the new function is called.

Grammar:

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

The parameter thisArg indicates that when the binding function is called, this parameter will be used as a pointer to this. This parameter is invalid when the binding function is called with the new operator.

Parameters arg1, arg2, …indicate that when the bound function is called, these parameters are passed to the bound method before arguments.

Let's start with an example:

this.name ="jack"; var demo={name:"rose",getName:function(){return this.name;}}console.log(demo.getName());//output rose where this points to demovar another=demo.getName;console.log(another())//output jack where this points to the global object//use the bind method to change this to var another2=another.bind(demo);console.log(another2());//output rose where this points to the demo object

Application of bind

You can preset initial parameters for a function:

function a(){return Array.prototype.slice.call (arguments);//Convert class array to real array}var b=a.bind(this,15,20)alert(b());/Popup 15, 20var s=b(25,30);alert(s);//Popup 15, 20, 25, 30

js bind multiple bindings are valid only the first time

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

The output is q1.

The above is "js bind how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report