


JavaScript call apply uses the method of the JavaScript object to bind to the DOM event after this points to the problem_javascript tips
Let’s take a look at the phenomenon first:
When the DIV is clicked, the pop-up box displays undefined.
The reason is that when the DOM object responds to the click event, the this keyword in the event method points to the DOM object. At this time, the DOM object does not have an attribute, so undefined pops up.
The programmer’s original intention is that this in the response event method points to object a of classA. How can this be achieved? This requires using the call or apply method.
Let’s get familiar with the call method:
Summary:
function.call(thisobj, args…)
Parameters:
thisobj
The object of the calling function. In the function body, thisobj is the value of the keyword this.
args…
Any number of parameters, these parameters will be passed to the function function.
Return value:
The return value of calling function function.
Throws:
TypeError
If the object calling the function is not a function, this exception is thrown.
Description:
call() calls the specified function function as the method of object thisobj, and passes the parameters after thisobj in the parameter list to it. The return value is the return value after calling the function. Within the function body, the keyword this refers to the thisobj object.
If the specified array is passed as a parameter to the function, please use the Function.apply() method.
After becoming familiar with the call() method, modify code 1 as follows:
Code 2:
function init() {
var el = document.getElementById("testDiv");
var a = new classA(el);
}
function classA(el) {
this.t = 1;
this.clickDele = createDele(this.click, this);
el.onclick = this.clickDele;
}
classA.prototype = {
click:function() {
alert(this.t);
}
}
function createDele(fun, obj, arg) {
return function() {
return fun.call(obj, arg);
}
}
Line 25 of code 2: The createDele method is mainly added. This method contains three parameters: fun, obj, and arg, which are respectively "the method to be executed" and "the object that this in fun needs to point to." , "Parameters passed into fun". This method returns an anonymous method.
The anonymous method is responsible for executing the fun method, pointing this in fun to obj, passing in parameters as arg, and processing the results to return.
When the program execution reaches line 15, the createDele method is called, passing in the object's method and the object itself. createDele returns an anonymous method after receiving the parameters. This.clickDele is set to the returned anonymous method. Line 16 of the code changes this. clickDele (anonymous method) is bound to the DOM event. After the program is executed, click on the DOM (DIV) to trigger the anonymous method. At this time, fun in the anonymous method is this.click (ie: method a.click) passed in before, and obj is This was passed in before (i.e. object a), so the call method is used at this time to make this in this.click (i.e. method a.click) point to obj (i.e. object a), and the final pop-up result is 1. The result is correct and the original intention of the program is achieved.
Looking back at anonymous methods makes people feel a little weird: why fun is this.click (ie: method a.click) and why obj is this (ie: object a) when calling anonymous methods. This problem needs to be explained using JavaScript closures. Closures will not be introduced here for now. An article introducing JavaScript closures will be published later. Interested friends are welcome to continue to pay attention!
Whether you believe it or not, there is no problem with the principles and procedures! :)

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
