JS function Note

Source: Internet
Author: User

Learning materials: http://www.runoob.com/js/js-function-definition.html

Javascriptfunction Definition

JavaScript uses the keyword function to define functions.

A function can be defined by a declaration, or it can be an expression.

Arguments Object

The JavaScript function has a built-in object arguments object.

The argument object contains an array of arguments for the function call.

In this way you can easily find the value of the last parameter

<!DOCTYPE HTML><HTML><Body><PID= "Demo"></P><Script>/*function MyFunction () {var i,max=0;for (i=0;i<arguments.length;i++) {if (Arguments[i]>max) {max=arguments[ I];}} return Max;} document.getElementById ("Demo"). Innerhtml=myfunction (4,5,6,33);*/functionSumAll () {varI,sum=0; for(i=0; I<Arguments.length;i++) {sum+=arguments[i];}returnsum;} document.getElementById ("Demo"). InnerHTML=SumAll (1,2,3,4,5)</Script></Body></HTML>
View Code

Closures:

<!DOCTYPE HTML><HTML><Body><Buttontype= "button"onclick= "myFunction ()">Fuck Me</Button><PID= "Demo"></P><Script>/*var counter=0;function add () {return counter+=1;} function MyFunction () {document.getElementById ("demo"). Innerhtml=add ();}*//*document.getElementById ("Demo"). Innerhtml=add (); function add () {var counter=0;function plus () {counter+=1;} Plus (); return counter;}*/varAdd=(function (){varcounter=0;return function(){returncounter+=1;}}) ();functionmyFunction () {document.getElementById ("Demo"). InnerHTML=add ();}</script<!--Variable add specifies the return character value of the function self-invocation. The self-invocation function executes only once. Set the counter to 0. and returns a function expression. The add variable can be used as a function. The great part is that it can access the counter of the scope of the function. This is called a JavaScript closure. It makes it possible for a function to have a private variable. Counters are protected by the scope of anonymous functions and can only be modified by the Add method. --></Body></HTML>
View Code

Closures are functions that can access variables in the scope of the previous function, even if the previous function has been closed.

JS function Note

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.