JavaScript self-learning notes (required) and javascript self-learning
0-determine whether variables and parameters are initialized
If (x) {} // The variable is initialized, the variable is not empty, or the variable is not zero.
1-declaring a function does not need to declare the return value, parameter type, or end of a sentence or even ';'
function sum(i1,i2){return i1+i2}
2-directly declare anonymous functions for immediate use
Var f = function (i1, i2) {return i1 + i2 ;}; alert (f (1, 2); // normal anonymous function alert (function (i1, i2) {return i1 + i2;} (3, 4); // directly declare and use
3-js does not have the class concept, so some methods are long like classes.
Function Person (name, age) {this. name = name; // Add attributes dynamically, similar to dynamic A = new ExpendoObject (); this. age = age; this. sayHello = function () {alert ('hello, My name is '+ name +' I '+ age + 'years old. ') };} var p1 = new Person ('lorry', 21); p1.SayHello (); // call p1.Gender = 'male' like a class '; // dynamically Add the 'gender' attribute alert (p1.Gender );
4-The Array object is an Array, and the length of the defined Array is not limited in advance.
var arr=new Array();arr[0]=0;arr[1]=1;arr[2]=2;for(var i=0;i<=arr.length-1;i++){alert(arr[i]);}
5-Array: Array, Dictionary, and Stack
Var dict = new Array (); // use dict ['I'] = 'wo' as a Dictionary; dict ['ai'] = 'ai '; dict ['you'] = 'ni'; alert (dict ['I']); // Call alert (dict. love); // call like calling attributes (Dynamic Language Features) for (var k in dict) {// traverse alert (k) in js; // 'me ', 'ay', 'you' --> Print the key} for (var k of dict) {// traverse alert (k) in js; // 'wo ', 'ai', 'ni' --> Print value} var arr = [, 5]; // simplified Array creation method var arr = {"lorry": 21, "cloud": 20}; // dictionary Creation Method
6-traverse all elements that can be called on the current page
Var s = null; for (var k in document) {// the attributes of the object are in the form of keys s + = k + ";" ;}alert (s );
7-use subscript operations similar to Array to obtain characters at a specified position of a string
Var s = 'hello, world! '; S [0]; // 'h's [6]; //'s [12]; //'! 'S [13]; // If the undefined index is out of the range, no error will be reported. However, all returned undefined indexes must note that the string is immutable. If you assign a value to an index of the string, there will be no errors, but there will be no effect: var s = 'test'; s [0] = 'X'; alert (s ); // s is still 'test'
8-change to uppercase and lowercase letters
Var s = 'hello'; s. toUpperCase (); // return 'hello' var s = 'hello'; s. toLowerCase (); // return 'hello'
9-search for the location where the specified string appears
Var s = 'hello, world'; s. indexOf ('World'); // return 7 s. indexOf ('World'); // The specified substring is not found and-1 is returned.
10-obtain the substring of the specified index range of the string
Var s = 'hello, world' s. substring (0, 5); // returns 'hello' s from index 0 to 5 (not including 5. substring (7); // returns 'World' from index 7 to index 7'
The 11-JavaScript object is an unordered set data type, which consists of several key-value pairs.
Var xiaoming = {name: 'xiaoming ', birth: 1990, school: 'No. 1 Middle School ', height: 1.70, weight: 65, score: null // the last key-Value Pair does not need to be appended with', '}; xiaoming. name; // 'xiaoming 'xiaoming. birth; // 1990 the access attribute is through. but this requires that the attribute name must be a valid variable name. If the attribute name contains special characters, it must be enclosed in []: var xiaohong = {name: 'xiaohong', 'middle-school': 'No. 1 Middle School '}; xiaohong ['middle-school']; // 'No. 1 Middle School 'xiaohong ['name']; // 'xiaohong' xiaohong. name; // 'xiaohong' xiaohong. age; // undefined
12-check whether xiaoming has a certain attribute. Use the in OPERATOR:
'Name' in xiaoming; // true 'grade 'in xiaoming; // false *** if in judges that an attribute exists, it is not necessarily xiaoming, it may be inherited by xiaoming: 'tostring' in xiaoming; // true *** to determine whether an attribute is owned by xiaoming rather than inherited, you can use hasOwnProperty () method: xiaoming. hasOwnProperty ('name'); // truexiaoming. hasOwnProperty ('tostring'); // false
13-Map
Var m = new Map ([['Michael ', 95], ['bob', 75], ['tracy', 85]); // two-dimensional array initialization method m. get ('Michael '); // 95 var m = new Map (); // Initialize an empty Mapm directly. set ('Adam ', 67); // Add a new key-valuem.set ('bob', 59); m. has ('Adam '); // whether the key 'Adam': truem. get ('Adam '); // 67m. delete ('Adam '); // delete key 'Adam' m. get ('Adam '); // undefined var m = new Map ([[1, 'x'], [2, 'y'], [3, 'Z']); for (var n of m) {// traverse Map alert (n [1] + '=' + n [0]);}
14-iterable's built-in forEach method, which receives a function and automatically calls back the function every iteration.
Var a = ['A', 'B', 'C'];. forEach (function (element, index, array) {// element: the value pointing to the current element // index: pointing to the current index // array: pointing to the Array object itself alert (element) ;}); Set is similar to Array, but Set has no index, so the callback function can have up to two parameters: var s = new Set (['A ', 'B', 'C']); s. forEach (function (element, set) {alert (element) ;}); Map callback function parameters are value, key, and map in sequence: var m = new Map ([[1, 'x'], [2, 'y'], [3, 'z']); m. forEach (function (value, key, map) {alert (value) ;}); var a = ['A', 'B', 'C'];. forEach (function (element) {alert (element );});
15-pass in our own function using the map () method of Array, and a new Array is obtained as the result:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];arr.map(function(x){return x*x;}).forEach(function (element) {alert(element);// [1, 4, 9, 16, 25, 36, 49, 64, 81]});
16-use map () to convert all numbers in Array into strings:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];arr.map(String); // ['1', '2', '3', '4', '5', '6', '7', '8', '9']
17-use reduce () of Array for cumulative Calculation
Var arr = []; for (var x = 1; x <= 100; x ++) {arr. push (x); // set 1 ~ 100 put in the array} alert (arr. reduce (function (x, y) {return x + y; // accumulate and sum all objects in arr, return the sum result }));
18-use reduce () for an awesome conversion: Convert [1, 2, 5, 8, 0] to an integer of 12580.
var arr = [1, 2, 5, 8, 0];alert(arr.reduce(function(x,y){return x*10+y;}));
19-filter () is used to filter out some elements of Array.
Var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; alert (arr. filter (function (x) {return x % 2 = 0, 8 // if the return value is true, the null String in an Array is retained. var arr = ['A', '', 'B', null, undefined, 'C ', '']; alert (arr. filter (function (s) {return s & s. trim (); // Note: The Versions earlier than IE9 do not have the trim () method}); // ['A', 'B', 'C']
20-by default, the sort () method of Array converts all elements into strings and then sorts them...
[10, 20, 1, 2]. sort (); // [1, 10, 2, 20]. Therefore, if you want to sort data by number, you can write var arr = []; for (var x = 1; x <= 10; x ++) {arr. push (x);} document. write (arr + "<br/>"); document. write (arr. sort (function (x, y) {return x <y? True: false;}); to ignore the case-sensitive influence of letters, convert the string to uppercase or lowercase var arr = ['Google ', 'apple', 'Microsoft']. alert (arr. sort (function (s1, s2) {var x1 = s1.toUpperCase (); var x2 = s2.toUpperCase (); return x1 <x2? False: true;}); // ['apple', 'Google ', 'Microsoft']
21-Closure program structure
① Assign a value to the parameter as the return value. Call this parameter to obtain the calculation result var arr = []; for (var n = 1; n <101; n ++) {arr. push (n);} function lazy_sum (arr) {var sum = function () {return arr. reduce (function (x, y) {return x + y;});} return sum;} var f = lazy_sum (arr); alert (f ()); ② The returned function is not executed immediately, but is not executed until f () is called. count () {var arr = []; for (var I = 1; I <= 3; I ++) {arr. push (function () {return I * I;});} return arr;} var results = count (); // three func instances are saved in results. Tionvar f1 = results [0]; var f2 = results [1]; var f3 = results [2]; f1 (); // The function returned by 16 references variable I, but it is not executed immediately. F2 (); // 16 when all three functions are returned, their referenced variable I has become 4, f3 (); // 16. Therefore, when returning a closure with a final result of 16 ***, remember that the return function should not reference any cyclic variables or variables that will change in the future! ③ What if circular variables must be referenced? The method is to create another function and bind the current value of the loop variable with the function parameter. No matter how the loop variable is changed, the value bound to the function parameter remains unchanged: function count () {var arr = []; for (var I = 1; I <= 3; I ++) {arr. push (function (n) {return function () {return n * n ;}} (I);} return arr ;}var results = count (); var f1 = results [0]; var f2 = results [1]; var f3 = results [2]; alert (f1 ()); // 1 alert (f2 (); // 4 alert (f3 (); // 9 ④ With the help of closures, you can encapsulate a private variable function creat_counter (init) {var n = Init | 0; return {add: function () {n + = 1; return n ;}}var c = creat_counter (); alert (c. add (); // 1 alert (c. add (); // 2 alert (c. add (); // 3 *** implements a closure in the returned object, which carries the local variable n and, variable n cannot be accessed from external code. In other words, a closure is a function that carries a state, and its state can be completely hidden. ⑤ Use Math. pow (x, y) calculates x ^ 2 or x ^ 3 // Math. pow (x, y) --> x ^ yfunction make_pow (y) {return function (x) {return Math. pow (x, y) ;}}var pow2 = make_pow (2) var pow3 = make_pow (3) alert (pow2 (3) // 9 alert (pow3 (3 )) // 27
22-arrow function (currently only supported by firefox) // parameter => function body
var f = x => x*x*xalert(f(3)) //27
23-generator generation of Fibonacci Series
Function * fib (max) {var t, a = 0, B = 1, n = 1; while (n <= max) {yield a; t = a + B; a = B; B = t; n ++;} return a;} for (var x of fib (10) {// use... of loop iteration generator object document. write (x + ''); // output 0, 1, 1, 2, 3} in sequence to generate an auto-increment ID (no global variable required) function * next_id () {for (var x = 1; x <100; yield x ++);} var g = next_id (); alert (g. next (). value); // 1 alert (g. next (). value); // 2 alert (g. next (). value); // 3
The above is all the content of the JavaScript self-taught notes (which must be read) provided by xiaobian. I hope you can provide more support to the customer's home ~