JavaScript Thin Notes

Source: Internet
Author: User
Tags delete key iterable new set sorts hasownproperty

JavaScript streamlined notes, excerpted from Liaoche's official website.

[From] https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000

' Use strict '; Strict mode

JavaScript is strictly case sensitive.

Nan = = = Nan; False

The only way to judge NaN is through a isNaN() function.

1/3 = = = (1-2/3); False

This is not a design flaw in JavaScript. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals. To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are less than a certain threshold:

Math.Abs (1/3-(1-2/3)) < 0.0000001; True

Multi-line string: ' ... '

Template string: ' Hello, ${name}, you ${age} year old! '

Action string: toUpperCase, toLowerCase, indexOf, substring

Array:

    vararr = [Ten, ' + ', ' xyz ']; Arr.indexof (10);//the index of element 10 is 0    vararr = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']; Arr.slice (0, 3);//starting at index 0, ending with index 3, but excluding index 3: [' A ', ' B ', ' C ']Arr.slice (3);//starting from index 3 to end: [' D ', ' E ', ' F ', ' G ']    vararr = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']; varAcopy = Arr.slice ();//Copying an arrayAcopy;//[' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']Acopy = = = Arr;//falsepush () adds several elements to the end of the array, and pop () removes the last element of the array.  If you want to add several elements to the head of an array, using the Unshift () method, the Shift () method deletes the first element of the array.  Sort () Sorts the current array, which directly modifies the position of the elements of the current array and, when called directly, sorts them in the default order.  Reverse () gives all the elements of the array, which is the reversal. vararr = [' Microsoft ', ' Apple ', ' Yahoo ', ' AOL ', ' Excite ', ' Oracle ']; //Delete the 3 elements starting at index 2, and then add the two elements:Arr.splice (2, 3, ' Google ', ' Facebook ');//return deleted elements [' Yahoo ', ' AOL ', ' Excite ']Arr//[' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']    //Delete only, do not add:Arr.splice (2, 2);//[' Google ', ' Facebook ']Arr//[' Microsoft ', ' Apple ', ' Oracle ']    //add only, do not delete:Arr.splice (2, 0, ' Google ', ' Facebook ');//return [] because no elements were removedArr//[' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']The concat () method joins the current array with another array and returns a new array. vararr = [' A ', ' B ', ' C ', 1, 2, 3]; Arr.join (‘-‘);//' a-b-c-1-2-3 '

Object:

If we want to detect whether Xiaoming has a property, you can use the in operator. ' Name ' in Xiaoming; True
To determine whether a property is owned by Xiaoming itself, but not inherited, you can use the hasOwnProperty () method. Xiaoming.hasownproperty (' name '); True

Condition Judgment:
JavaScript treats null, undefined, 0, Nan, and empty string ' ' as false, and all other values are treated as true.

Cycle:
A variant of the For loop is a for ... in loop, which loops through all the properties of an object in turn. To filter out the attributes inherited by the object, use hasOwnProperty () to implement.
Because an array is also an object, and its index of each element is treated as an object's property, the for ... in loop can directly loop out the index of the array.

MAP:

Map is the structure of a set of key-value pairs with extremely fast lookup speeds.  Since a key can only correspond to one value, the value is placed on a key more than once, and subsequent values flush out the previous value. varm =NewMap ([' Michael ', ' up], [' Bob ', []], [' Tracy ', 85]]); M.get (' Michael ');// the    varm =NewMap ();//Empty MapM.set (' Adam ', 67);//Add a new Key-valueM.set (' Bob ', 59); M.has (' Adam ');//is there a key ' Adam ': trueM.get (' Adam ');// theM.Delete(' Adam ');//Delete key ' Adam 'M.get (' Adam ');//undefined

Set:
A set is similar to a map and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.
You can add elements to the set by using the Add (key) method, but you can add them repeatedly, but it won't work.
You can delete an element by using the Delete (key) method.
var S1 = new Set (); Empty set
var s2 = new Set ([1, 2, 3]); Including 1, 2, 3

Iterable:

traversing an array can take the subscript loop, and traversing the map and set cannot use the subscript. To unify the collection type, the ES6 standard introduces a new iterable type, with Array, map, and set belonging to the iterable type.    A collection with the iterable type can be traversed by a new for ... of loop. varA = [' A ', ' B ', ' C ']; A.name= ' Hello ';  for(varXincha) {alert (x);//' 0 ', ' 1 ', ' 2 ', ' name '    } forThe. in loop will include name, but the length property of the array is not included.  forThe . of cycle completely fixes these problems, and it only loops the elements of the collection itself. varA = [' A ', ' B ', ' C ']; A.name= ' Hello ';  for(varX of a) {alert (x);//' A ', ' B ', ' C 'the better way is to directly use the iterable built-in foreach method, which receives a function that automatically callbacks each iteration. varA = [' A ', ' B ', ' C ']; A.foreach (function(element, index, array) {//element: A value that points to the current element        //Index: Point to Current index        //array: Points to the array object itselfalert (element); }); Set is similar to array, but set has no index, so the first two parameters of a callback function are the elements themselves:vars =NewSet ([' A ', ' B ', ' C ']); S.foreach (function(element, sameelement, set) {alert (element); }); The callback function parameters for map are value, key, and map itself:varm =NewMap ([[1, ' X '], [2, ' Y '], [3, ' Z ']]); M.foreach (function(value, key, map) {alert (value); });

JavaScript Thin Notes

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.