Understanding JavaScript Functions

Source: Internet
Author: User
Document directory
  • Function Definition
  • Function Parameters
  • No overload

All those who have learned programming know about functions. functions can encapsulate any number of statements and can be called and executed anywhere and anytime. The same is true for functions in JavaScript, however, it is different from functions in other programming languages.

  • You do not need to specify the return value of the function, because the JavaScript function can return any value at any time.
  • In fact, a function that does not return a Value Returns a special undefinded value.
  • You can pass any number of parameters to JavaScript Functions, because their function parameters are passed in the form of an array containing zero or multiple values, and they can be accessed through the arguments object.
  • Javascript functions cannot be overloaded.

JavaScript Functions are actually function-type instances, so functions are also objects, which is the most distinctive feature of JavaScript. Because functions are objects, functions also have methods, can be used to enhance its behavior.

Function Definition

JavaScript Functions are declared using the function keyword, followed by a set of parameters and function bodies:

function functionName(arg0, arg1, ... , argN){    statements}

When defining JavaScript Functions, you do not need to specify whether to return values. In fact, any function can return values at any time through the return statement followed by the value to be returned, as shown in

function sum(num1, num2){    return num1 + num2;}var result = sum(6, 8);

The function stops and exits immediately after the return statement is executed. Therefore, any code after the return statement will never be executed.

Function sum (num1, num2) {return num1 + num2; alert ("Hello world! "); // Never executed}

A function can also contain multiple return statements.

function diff(num1, num2){    if(num1 < num2){        return num2 - num1;    }else{        return num1 - num2;    }}

Otherwise, the return statement does not return any values. In this case, the function returns the undefinded value after it is stopped. This method is generally used when you need to stop the function in advance without returning a value.

Function sayhi (name, message) {return; alert ("hi" + name + "," + message); // never run}

Function Parameters

The parameters of JavaScript Functions are different from those of most other languages. The parameters of JavaScript functions do not mind how many parameters are passed in, nor do they care about the type of parameters passed in, even if you only receive two parameters when defining a function, you may not have to pass two parameters when calling this function. You can pass one, three, or even none, this is because JavaScript parameters are represented by an array internally, and the function always receives this array, regardless of the parameters contained in the array. In fact, you can access this parameter array through the arguments object in the function body to obtain every parameter passed to the function.

Each element of an arguments object can be accessed using square brackets, that is, the first element is arguments [0], the second element is arguments [1], and so on, you can use the Length attribute to obtain the number of passed parameters.

For example, you can use this to enable the function to receive any parameter and implement the appropriate functions respectively.

function doAdd(){    if(arguments.length == 1){        return arguments[0] + 10;    }else if(arguments.length == 2){        return arguments[0] + arguments[1];    }}doAdd(10);  //20doAdd(10, 20);  //30

No overload

JavaScript Functions cannot implement overloading as they do in the traditional sense. In other languages such as Java, you can write two definitions for a function, as long as the parameters and quantities of these two definitions are different, however, JavaScript function parameters are represented by arrays containing zero or multiple values, so they cannot be overloaded.

If two functions with the same name are defined in Javascript, the name only belongs to the latter-defined function, because the latter-Defined Function overwrites the first-defined function.

function addNum(num){    return num + 100;}function addNum(num){    return  num + 200;}var result = addNum(100);  //300

By checking the type and quantity of parameters in the input function and making different responses, the overload of the method can be imitated.

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.