Function (2) _ JavaScript skills in javascript

Source: Internet
Author: User
A function is an event-driven or reusable code block that is executed when it is called. This article introduces functions in javascript (2 ), if you are interested in javascript Functions, learn about them. functions are event-driven or reusable code blocks executed when they are called.

JavaScript function syntax

A function is a block of code enclosed in curly braces. The keyword function is used before:

Function functionname () {the code to be executed here}

When this function is called, the code in the function is executed.

A function can be called directly when an event occurs (for example, when a user clicks a button), and JavaScript can call the function anywhere.

Tip: JavaScript is case sensitive. The keyword function must be in lower case and must be in the same case as the function name.

1. Function Scope

Scope refers to the range in which variables exist. Javascript has two scopes. One is global scope. Variables always exist in the entire program, and the other is function scope. variables only exist in the function body. The variable declared outside the function body is a global variable, which can also be read inside the function body.

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

This is a global variable and can be used inside the function body.

function f(){  var v = 1;}

This is a local variable and cannot be read from outside the function body.

2. Closure

A closure is a function defined inside the function body.

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

In the appeal code, c is defined in function body f, and c is the closure.

The closure feature is 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, originally outside function f, we cannot read the internal variable v. However, with the help of closure c, you can read this variable.

The closure can not only read the internal variables of the function, but also make the internal variables remember the operation results of the last call.

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

The B variable in the function is calculated based on the value of the previous call.

The above is a full description of the functions (II) in JavaScript introduced by the editor. I hope you will like it.

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.