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
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
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