Examples to explain the usage of JavaScript function binding
This article mainly introduces the usage of JavaScript function binding, and analyzes the principles, implementation methods and related operating techniques of JavaScript function binding in the form of examples. Friends who need it can refer to it. I hope it can help everyone.
Perface
If you are asked to implement this page and some operations, such as clicking 1, 2 , 3, etc. are displayed in the input text, and there is also a delete function. We don’t care about it first, it is just a simulation. If I just started doing it, I would do this:
1. Use css and HTML to layout the interface
2. Use javascript event delegation to listen to the click event of the parent node of the button
But what if I want to use object-oriented thinking? I did it in Ext, so I would say it helped me encapsulate a lot. Maybe some people who have never used Ext don't understand the code I posted below, but I will try to explain it clearly!
Description
##
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
handler: this.press in the listening event. The problem I encountered is what if I need to use some attributes of this ContactTelPanel class in the press function?
Idea
I am thinking that I want to use the attributes of this class in the press function. I use this object directly in press. Just get it, but I was wrong. For example, if you useconsole.dir(this) in the press function, see what appears on the chrome console? Unfortunately, what appears is a Button object, and its The this pointer has changed. It was indeed a bit troublesome, and then I thought of three methods, as follows:
Solution
1 Pass in each function that listens to the event ParameterCode: handler: this.press(this), then write
alert(arguments[0]) in the press function body
Situation: It is true that the pop-up window is ContactTelPanel when this page is loaded, but it does not appear when you click those buttons
The reason : this.press(this), the javascript parser written in this way will be regarded as calling the press function, and then executed when you load the page
Code: For example, set me = this on the fifth line, and then write alert(me)
: It is indeed possible to pop up a window when clicking the button. Successful
Disadvantages: Global variables are polluted and easily tampered with by others unintentionally. For example, when I introduce the js of this page and then reference other js, I set var me = "monkindy" in the subsequent js. Then when you click the button of that page, it will pop up 123, which is not a ContactTelPanel object3 Simple use of closure
Code:handler: function(){me.press(me)} Note: me is the ContactTelPanel object, Because the this pointer in the function is already a button object, the this object should be saved with me (or other variable name) outside the function, that is, var me = this
: This is of course a success4 Use call to implement function binding
Code:handler: function(){ me.press.call(this,me );}
: This should be the most usefulAnother: The previous article explains JavaScript function binding in detail based on pure js Function binding technology, interested readers can compare the similarities and differences between the two.
Related recommendations:
Detailed explanation of how to use function binding code for javascript interaction##js uses function binding technology to change event handlers Scope_javascript skills
Detailed explanation of JavaScript function binding_Basic knowledge
The above is the detailed content of Examples to explain the usage of JavaScript function binding. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

In today's era of information explosion, the construction of personal brand and corporate image has become increasingly important. As the leading fashion life sharing platform in China, Xiaohongshu has attracted a large number of user attention and participation. For those users who want to expand their influence and improve the efficiency of content dissemination, binding sub-accounts has become an effective means. So, how does Xiaohongshu bind a sub-account? How to check whether the account is normal? This article will answer these questions for you in detail. 1. How to bind a sub-account on Xiaohongshu? 1. Log in to your main account: First, you need to log in to your Xiaohongshu main account. 2. Open the settings menu: click "Me" in the upper right corner, and then select "Settings". 3. Enter account management: In the settings menu, find the "Account Management" or "Account Assistant" option and click

1. Open Toutiao. 2. Click My in the lower right corner. 3. Click [System Settings]. 4. Click [Account and Privacy Settings]. 5. Click the button on the right side of [Douyin] to bind Douyin.

The Cainiao app is a platform that can provide you with various logistics information. The functions here are very powerful and easy to use. If you have any logistics-related problems, they can be solved here. Anyway, it can bring you a The one-stop service can solve everything in time. Checking the express delivery, picking up the express delivery, sending the express delivery, etc. are all without any problems. We have cooperated with various platforms and all the information can be queried. However, sometimes It will happen that the goods purchased on Pinduoduo cannot display the logistics information. In fact, you need to manually bind Pinduoduo to achieve this. The specific methods have been sorted out below, and everyone can take a look. . How to bind Cainiao to Pinduoduo account: 1. Open Cainiao APP and go to the main page

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Do you know how to bind Pinduoduo when using Cainiao Wrap? The official version of Cainiao Wrap App does not automatically synchronize some Pinduoduo’s logistics information on this platform. All we need to do is You can copy the order number or check your mobile phone to see if there is any express delivery information. Of course, these all need to be completed manually. If you want to know more, come and take a look with the editor. How to bind the Cainiao APP to Pinduoduo 1. Open the Cainiao APP and click "Package Guide" in the upper left corner of the main page. 2. In the interface, there are many shopping websites, and accounts can be bound. 3. Click to import other e-commerce platforms. 4. User authorization: Click Pinduoduo to go to the interface
