JavaScript Basics-1

Source: Internet
Author: User
Tags variable scope

first, the function

There are three ways to define a function:

function FnName (args) {}

var fnname = function (args) {}

var fnname = new Funtion (args, function body);

1. Arguments objects

(1)? ? Simulate function overloading ? ?

function Add () {if (arguments.length==2) {return arguments[0] + arguments[1];    }else if (arguments.length==3) {return arguments[0] + arguments[1] + arguments[2];  }}alert (Add (2,3));   Output: 5alert (Add (2,3,4)); Output: 9

2. Variable Scope

There are two scopes in JS: global domain and function domain, which correspond to global variables and local variables respectively. Note: There is no block-level scope in JS, that is {}. A variable defined in a code block, such as an if or for statement, is visible outside the block of code.

1) do not use the var keyword when defining a local variable: The local variable is defined as a global variable 2) Only local variables can be accessed in the function field when the global variable has the same name as the local variable

3. Special functions

1) anonymous function

    1. Anonymous functions can be passed as arguments to other functions. In this way, the receiver function can use the passed function to accomplish something--Anonymous callback function

    2. You can define an anonymous function to perform some one-off tasks –> self-tuning function

2) callback function

Function II (A, B, C, callback) {var i, arr = [];   for (int i = 0, i < 3, i + +) {Arr[i] = callback (arguments[i] * 2); } return arr;}

3. Self-tuning function

(function () {alert ("JavaScript"); })();   -------------------(function (name) {alert (name + "I love you!"); }) ("XT");

4. Predefined global functions

Encodeuri/decodeuri: Encoding and decoding

Encodeuricompent/decodeuricompent : Encoding and decoding

。。。。。

JavaScript Basics-1

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.