function
Directly on the code!
d = var new new a Date (); // getDate () Gets Day // getDay () Gets week // getMonth () Gets month (0-11 ) // getFullYear () for a full year // getYear () Gets Year / / getHours () Gets h // getMinutes () Gets min // the getSeconds () Gets seconds // the getMilliseconds () Gets milliseconds // getTime () returns the total number of milliseconds (from midnight 1970/1/1)
// normal function definition function F1 () { the console.log ( " the Hello World! " ); } // function arguments function F2 (A, B) { the console.log (arguments); // built arguments object console .log (the arguments.length); the console.log (a, B); } // return value of the function function SUM (a, B) { return a + B; } SUM ( . 1, 2); // call the function // anonymous functions embodiment var SUM = function (A, B) { return A + B; } SUM ( . 1, 2 ); //Immediate execution function (function (A, B) { return A + B; }) ( . 1, 2);
argument
argument, is used to receive the js plurality of parameters, you can not even write parameter, but in order to distinguish without parameters, or to bring the general parameter.
function add(a,b){ console.log(a+b); console.log(arguments.length) } add(1,2) 结果: 3 2
Global variables and local variables of the function
Local variables
In JavaScript variables declared inside a function (using var) it is a local variable, so it can only be accessed inside the function (the scope of the variable is the internal function). As long as the function is completed, the local variable will be deleted.
Global Variables
Variables declared outside a function are global variables, all on a Web page scripts and functions can access it.
Variable life cycle:
Lifetime JavaScript variables from the time they are declared.
Local variables will be deleted after the function is run.
Global variables will be deleted after the page is closed.
Scope
First look for variables inside a function, can not find the outer function to find, and gradually find the outermost layer.
Look at a few examples:
1,
var city = "BeiJing"; function f() { var city = "ShangHai"; function inner(){ var city = "ShenZhen"; console.log(city); } inner(); } f(); #shenzhen
2,
var city = "BeiJing"; function Bar() { console.log(city); } function f() { var city = "ShangHai"; return Bar; } var ret = f(); ret(); #Bejing
3, closure
var city = "BeiJing"; function f(){ var city = "ShangHai"; function inner(){ console.log(city); } return inner; } var ret = f(); ret(); #shanghai
lexical analysis
At that moment in JavaScript function is called, it will carry out a lexical analysis.
Lexical analysis process:
When the function is called before the moment, will first form a activate an object: Avtive Object (AO), and will analyze the following three aspects:
1: function parameters, if any, assigned to this parameter AO, and is undefined. If not, then do nothing.
2: function local variable with the same name if the AO, no operation. If not, this variable is assigned to the AO, and the value is undefined.
3: The function declaration, if the AO, will be covered by objects on the AO. If not, then do nothing.
Whether using the internal function parameters or local variables are looking to use the AO.
Built-in objects and methods
Everything is an object in JavaScript: strings, numbers, arrays, date, etc. In JavaScript, the object is to have the properties and methods of data.
Custom object
Certain aspects of dictionary-like data types in python
A = {var " name " : " Alex " , " Age " : 18 is }; JS in the subject, the key (attributes) by default without quotes; single quotes and automatically turn double quotes console.log (a.name ); the console.log (A [ " Age " ]); # Alex # 18 is
Traverse the content object:
var a = {"name": "Alex", "age": 18}; for (var i in a){ console.log(i,a[i]) }
Create an object:
var person = new Object (); // Create a person object PERSON.NAME = " Alex " ; // person object name attribute person.age = 18 is; // object person age property
Date Object
Create a Date object
// Method 1: no parameter var D1 = new new a Date (); the console.log (d1.toLocaleString ()); // Method 2: a date parameter string var D2 = a Date new new ( " 2004/3/20. 11 : 12 is " ); the console.log (d2.toLocaleString ()); var D3 = a Date new new ( " 04/03/20 11:12 " ); the console.log (d3.toLocaleString ()); // method 3: parameter is the number of milliseconds var D3 = new new a date (5000 ); the console.log (d3.toLocaleString ()); the console.log (d3.toUTCString ()); // method 4: The date parameter hours minutes seconds milliseconds var D4 = new new a Date (2004,2,20,11,12,0,300 ); the console.log (d4.toLocaleString ()); // ms does not directly display
Date object methods:
d = var new new a Date (); // getDate () Gets Day // getDay () Gets week // getMonth () Gets month (0-11 ) // getFullYear () for a full year // getYear () Gets Year / / getHours () Gets h // getMinutes () Gets min // the getSeconds () Gets seconds // the getMilliseconds () Gets milliseconds // getTime () returns the total number of milliseconds (from midnight 1970/1/1)
JSON object
S = var ' { "name": "Xiaoqiang", "Age":} 38 is ' ; // convert the string to an object inside JS var RET = the JSON.parse (S); the console.log (RET); Console .log (typeof RET); // convert the object into a string inside JS var S2 = the JSON.stringify (RET); the console.log (S2); the console.log (typeof S2);
RegExp object
// RegExp objects // Create a regular target mode 1 // parameter 1 regular expression (no space) // Parameter 2 Match Mode: Common g (global matching; find all matches rather than stopping after the first match) and i (ignore case) // user name can only be letters, numbers and _ and must be the first letter of the English alphabet. Not be less than the minimum length of six not be longer than 12 bits. // Create RegExp object mode (no space behind the comma) var REG1 = new new RegExp ( " ^ [A-zA-the Z] [A-zA-Z0-9 _] {5,11} $ " ); // match the response of string var S1 = " bc123 " ; // test method RegExp object, whether the test string corresponding to a regular rule, the return value is true or false. reg1.test (s1); // to true // Create a way 2 // / fill regular expression / matching pattern (no space after the comma) var reg2= / ^ [A-zA-the Z] [A-zA-Z0-9 _] {5,11} $ / ; reg2.test (S1); // to true // . 4 th String object with the regular method of binding var s2 = " Hello World " ; s2.match ( / O / G); // [ " O " , " O " ] Find string that meet the regular content s2.search ( / H / G); // 0 Find character string that match the regular expression of the content location s2.split ( / O / G); // [ " Hell " , " W " , " RLD " ] cutting the string according to a regular expression s2.replace (/ O / g, " S " ); // " Hells wsrld " strings according to a regular replacement // of the matching modes: the simple example of g and i var S1 = " name: Alex Age: 18 is " ; s1.replace ( / A /, " ha " ); // " n-ha me: Alex Age: 18 is " s1.replace ( / A / G, " ha " ); // " n-ha me: Alex ha GE: 18 is " global matching s1.replace ( / A / GI, " ha " ); // " n-ha me:Ha ha ha ha ha ha lex GE: 18 " Case-insensitive // Note 1: // If regExpObject with a global flag g, test () function is not to start searching at the beginning of the string, but start looking for property regExpObject.lastIndex from the specified index. // value of this property defaults to 0, so the first time is still searching at the beginning of the string. // When a match is found, the value of test () function will regExpObject.lastIndex string changed to the next position of this index of the last character of the matching content. // When the function will start looking at the index position from executed test again () to find the next match. // So when we use the test () function performs a match, if you want to re-use the test () function to start from scratch to find, you need to manually reset the value regExpObject.lastIndex is zero. // If the test () function can no longer find text matches, the function will automatically reset to 0 regExpObject.lastIndex property. REG3 var = / foo / G; // this time = regex.lastIndex 0 reg3.test ( ' foo ' ); // Returns to true // case regex.lastIndex = 3 reg3.test ( ' foo ' ); // returns to false // so we use the test () method of checking whether a string exact match is not recommended to add the global pattern matching g. // Note 2 (say you may not believe Series): // when we do not add a parameter called RegExpObj.test () method, equivalent to the implementation RegExpObj.test ( " undefined " ), and / undefined / .test () The default returns true. REG4 var = / ^ $ undefined / ; reg4.test (); // Returns to true reg4.test (undefined); // Returns to true reg4.test ( " undefined " ); // Returns to true the RegExp
Math object
abs (x) returns the absolute value of. exp (x) returns the exponential of e. floor (x) of the number is rounded. log (x) returns the natural logarithm (base e). max (x, y) returns the maximum of x and y values of. min (x, y) returns the lowest value of x and y. pow (x, y) returns the y-th power of x. random () returns 0 ~ 1 random number between. round (x) the number rounded to the nearest integer. sin (x) returns the sine. sqrt (x) returns the square root of the number. tan (x) returns the angle of the tangent. Math