Functions in JavaScript

Source: Internet
Author: User

The JavaScript scripting language allows developers to combine reusable blocks of code by writing functions, increasing the structure and modularity of scripting code. Functions are data passing through parameter interfaces to achieve specific functions.

First, the creation and invocation of functions

There are roughly three ways to create a function:

Method One: function functionname ([parameters]) {functionbody};

Example:

function Add (b) {    return a + b;} Alert (Add (2,3));

When we declare a function like this, the contents of the function are interpreted (only when the function is called), and an object named add is created.

Method Two: Assign an anonymous function to a variable (VAR)

Example A:

var add = function (A, b) {    alert (a+b);} Add (3,4);

The syntax for this statement looks weird, but it's better for us to understand "function as Object". Functions declared in this way are also executed without being executed.

Example B:

var obj = new Object (); obj.add = function (A, b) {    alert (a+b);} Obj.add (1,10);

This example shows that when we need to use a custom function as a property of an object, the way this function is defined is very useful.

Method Three: Create Functions from Function objects

Syntax: var name=new Function ([Param1name, Param2name,... paramnname], functionbody);

var add = new Function ("A", "B", "Alert (a+b)");//add (2,4);

A function is an object (the object that creates it). In fact, declaring a function in JavaScript is essentially creating an instance of the function object, and the name of the functions is the instance name.

Second, the parameters of the function

Unlike other programming languages, JavaScript does not verify that the number of arguments passed to a function is equal to the number of arguments defined by the function. If the number of arguments passed is different from the number of arguments defined by the function, then the function may produce some unexpected errors, and any missing arguments will be passed to the function with undefined, and the extra arguments are ignored. To avoid errors, the number of arguments passed should be equal to the number of parameters defined by the function. A arguments array object is provided in JavaScript that can get the arguments passed to the function from the JavaScript code.

Example:

function add (x, y) {    if (arguments.length! = 2) {        var str = "Passed the wrong number of arguments, a total of" + arguments.length + "parameters, respectively: \ n"; 
   
    for (var i = 0; i < arguments.length; i++) {            str + = "First" + (i + 1) + "parameters are:" + arguments[i] + "\ n";        }        return str;    }    else {        var z = x + y;        return z;    }   } Console.log ("Add (2,4,6):" +add (2,4,6));
   

Operation Result:

Note: You can use typeof to determine the type of function parameter passing.

Properties and methods of functions

  

  

 

  

  

Functions in JavaScript

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.