JS basics-create and use functions

Source: Internet
Author: User
Tags add numbers

JS basics-create and use functions

In JavaScript, functions are essentially similar to the functions in programming languages we usually learn. They also have common functions such as function names, parameters, return values, and function bodies. But as a scripting language, it does have its own differences.

I. Create

Function box () {// use the function keyword to declare + function name + (you can add parameters) + {Specific execution content, including the return value} return 'Hello! ';}

Here, we need to know that the () following the box has the function of executing this function, and can be used for passing parameters at the same time.

If we call alert (box) directly in JS, the result will be shown as: because no () is added, it is not executed. 'Hello! '(This is used in anonymous functions)



Ii. Function Parameters

The JS function uses an arguments object to dynamically transmit parameters so that the function does not mind the number of parameters when passing parameters!

1. simple use of the arguments object

Function box () {// The return arguments [0] + arguments [1] + arguments [2] is not passed here; // The number of parameters here} alert (box ('Everybody ', 'National Day', 'happy! '));

It can be seen that the arguments object is equivalent to an array of objects, which puts the parameters passed in the function in an array and calls them in order.

If it is called, the length is 3, that is, the number of parameters.

function box(){return arguments.length;}
However, there is a problem. Here there is no dynamic parameter passing, but the parameters are exactly the same. In fact, the arguments object can only recognize the number of parameters it has defined in advance, if a parameter is not passed in, it is automatically replaced by undefined. If the number of parameters is exceeded, the parameter will be erased automatically and will not be displayed!
How can we achieve dynamic parameter passing? So that the above function can be passed in and recognize all the parameters that I want to pass in. For example, when implementing addition, I don't know how many users add numbers. How can I solve this problem? A simple loop is used here.

2. dynamically passing parameters using the arguments object:

Function box () {var sum = 0; if (arguments. length = 0) return sum; for (var I = 0; I, if we call the function alert (box (3, 4, 5, 6, 7, 8, 9 )), you can get the desired answer no matter how much you add. It should be noted that if the string format exists, the number will also be treated as a string, and '+' will be treated as a & hyphen.

Iii. Anonymous Functions

Functions in JS also support unknown heroes. When declaring a function, a function without a function name is called an anonymous function ).

function (){return 'hello!';}

But how can I call a function without a function name?

How to execute an anonymous function:

1. Call by assigning a variable

var box=function(){return 'lee';}alert(box()); 

2. self-execution

When creating an anonymous function, I mentioned that () represents execution, and the same is true for anonymous functions.

(function(){  alert( 'lee');})() 
// Syntax: (anonymous function) () The first parentheses are placed in the anonymous function, and the second parentheses are executed.
Here () enables the anonymous function in JS to be directly executed when the page is loaded. No value assignment is required.
3. Passing parameters using anonymous Functions

Here we also use the () knowledge. The second () represents execution and can also transmit parameters. Therefore, we can also transmit parameters for anonymous functions.

For example:

(function(age){alert(age);         }(100)) 
In fact, here is the same as our usual function principle. You can regard the previous anonymous function as a function name, followed by parentheses for parameter passing and execution. Here, 100 can be replaced by other expressions, such as the values of some expressions obtained on the page.

The above are some basic knowledge about functions in JS learning. It is necessary to grasp the similarities and differences between the original knowledge and the current content, and carefully summarize and repeat it to make the application possible. There are still many applications for using anonymous functions, which will be further summarized in the future!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.