SoFunction
Updated on 2025-03-01

Javascript createAdder function function and instructions

Original English
createAdder(x) is a function that returns a function. In JavaScript, functions are first-class objects: they can be passed to other functions as arguments and returned from functions as well. In this case, the function returned is itself a function that takes an argument and adds something to it.

Here’s the magic: the function returned by createAdder() is a closure. It “remembers” the environment in which it was created. If you pass createAdder the integer 3, you get back a function that will add 3 to its argument. If you pass 4, you get back a function that adds 4. The addThree and addFour functions in the above example are created in this way.

Let’s take another look at the addLoadEvent function. It takes as its argument a callback function which you wish to be executed once the page has loaded. There follow two cases: in the first case, does not already have a function assigned to it, so the function simply assigns the callback to . The second case is where the closure comes in: has already had something assigned to it. This previously assigned function is first saved in a variable called oldonload. Then a brand new function is created which first executes oldonload, then executes the new callback function. This new function is assigned to . Thanks to the magical property of closures, it will “remember” what the initial onload function was. Further more, you can call the addLoadEvent function multiple times with different arguments and it will build up a chain of functions, making sure that everything will be executed when the page loads no matter how many callbacks you have added.

Closures are a very powerful language feature but can take some getting used to. This article on Wikipedia provides more in-depth coverage.

Chinese translation: If you have better ones, please leave a message. The general meaning is almost the same

createAdder(x) is a function that returns a function. In JavaScript, functions are the first type of objects: in addition they can be passed to other functions as parameters and returned as functions. In this case, the function return itself is a function that accepts a parameter and adds something.

Here, Äôs the magic: The function() returned by createAdder is a closure. It, Äúremembers, the AU is creating its environment. If you pass createAdder integer 3, you return a function that will increase 3 to its argument. If you pass four, you come back a function and add 4. The addThree function in the example above creates such a addFour function.

Let the Avenue of Stars take a look at the addLoadEvent function again. This requires the execution of the page that has been loaded as a callback function parameter to your desire. There are two cases: In the first case, a function has not been assigned to it, so the function is simply called back in the allocation. The second case is when it is closed: there is already something assigned to it. This is the first time a previously allocated feature was saved in a variable called oldonload. Then, a brand new function is to create the first execution of the oldonload and then execute the new callback function. This new feature is assigned in. Magical blockade property thanks to it, it will Äúremember, AU's original onload feature. Further, you can call the function's addLoadEvent multiple times with different parameters, which will create a functional chain, ensuring that everything will be executed when the page is loaded, regardless of how many callbacks you have increased.

Closures are a very powerful language feature, but may take some time to get used to. This provides a deeper coverage of Wikipedia articles.

Core code
Copy the codeThe code is as follows:

function createAdder(x) {
return function(y) {
return y + x;
}
}
addThree = createAdder(3);
addFour = createAdder(4);
('10 + 3 is ' + addThree(10) + '<BR>');
('10 + 4 is ' + addFour(10));
('-10 + 4 is ' + addFour(-10));


Demo code:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]