Functions in JavaScript (ii) _javascript tips

Source: Internet
Author: User

A function is an event-driven or reusable block of code that executes when it is invoked.

JavaScript function syntax

A function is a block of code wrapped in curly braces, preceded by a keyword function:

function functionname ()
{
Here is the code to execute
}

When the function is called, the code inside the function is executed.

You can call a function directly when an event occurs (for example, when a user clicks on a button), and it can be invoked anywhere by JavaScript.

Tip: JavaScript is sensitive to case sensitivity. The keyword function must be lowercase and must be called with the same case as the function name.

1. Scope of functions

Scopes refer to the scope of the existence of a variable. There are two scopes in JavaScript, one is the global scope, the variable exists throughout the program, the other is the function scope, and the variable exists only within the function body. A variable declared outside a function body is a global variable and can also be read inside a function body.

var v = 1;
function f () {
   console.log (v);
}
f ();

This is the global variable, which can also be used inside the function body.

function f () {
  var v = 1;
}

And this is the local variable, the function can not be read outside the body.

2. Closure of the package

A closure is a function defined within a function body.

function f () {
var c = function () {}; 
}

In the appeal code C is defined in the function body F, C is the closure.

The closure is characterized by the fact that variables inside the function body can be read outside the function body.

function f () {
var v = 1;
var c = function () {return
V;
};
return c;
}
var o = f ();
O ();
1

The code above shows that we were not able to read the internal variable v outside the function f. However, with the help of closure C, you can read this variable.

Closures can not only read function internal variables, but also enable internal variables to remember the last call when the results of the operation.

function f (b) {return
function () {return 
b++;
}
var b= f (5);
B ()//5
B ()//6
B ()//7

The B-variable inside the function, and each call is computed on the basis of the value of the last call.

The above is a small set of JavaScript in the description of the function (ii) of the whole description, I hope you like.

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.