This article is used for the rookie level like me, big guy see in the code is not easy, give a praise (welcome to point out the wrong and guidance), some of their own writing, some of the trouble to copy, this article is mainly used for later content review
Updated continuously ~
Js based
-
1. Variable scope — global and local variables
var str1 = 'global'; str2 = 'global'; function myFunction() { var str1 = 'local'; return str1; } function myOtherFunction() { str2 = 'local'; returnstr2; } console.log(str1); //{1} console.log(myFunction()); //{2} console.log(str2); //{3} console.log(myOtherFunction()); //{4} console.log(str2); / / {5}Copy the code
{1}– output global because it is a global variable
{2}– output local, str1 is declared in myFunction, so it only works in the scope of the function itself
{3}– output global,str2 is assigned to the second line
{4}– print local, because without var it is equivalent to str2 making a global assignment in its own function scope
>>> The difference between variables and attributes: the former can not be deleted, the latter can be deleted! For example, delete str1 cannot be deleted, but delete str2 can be deletedCopy the code
{5}– str2 is local because {4} has been reassigned
Note: Minimize global variables in your code, or use lets
-
2. Operators — ternary operators
Ternary operator
let a = 1 let b = 2 let c = 3 letd = 4 //1. p = a == 1 ? A: b //a = 1, p=a, otherwise p=b //2. Case, but it's hard to understand p = a == B, right? 1 : (a == c ? a : (a == d ? A: d)) // whether a is equal to b, if so,p= 1. If not, whether A is equal to C, if so,p=a. If not, is a equal to d, if p is equal to a, if not p is equal to dCopy the code
-
True or false — here directly posted for reference
Numeric types Converts to a Boolean value undefined false null false Boolean value True is true, false is false digital +0, -0, and NaN are false, and all others are true string False if the string is empty (length 0), true for everything else object true
An array of
1. Fibonacci numbers
Given that the first number is 1, the second number is 2, and the third number is the sum of the first two numbers, the list of the first 20 numbers is given arr[0] = 1, arr[1] = 2; Arr [I] = arr[i-1] + arr[i-2] then it is easy:Copy the code
letArr = [1, 2]for (let i=2; i<20; i++) {
arr[i] = arr[i-1] + arr[i-2]
}
Copy the code
As a result,
2. Component insert value
Inserting a value at the beginning of an array is easy if you use unshift, just arr. Unshift (number) now implements this in the original way, by moving all the values back one bit and assigning arr[0]Copy the code
let arr = [1, 2, 3, 4, 5]
let newArr = [10]
for (leti = arr2.length; i>0 ; i--) { newarr[i] = arr2[i-1] }Copy the code
Results:
3. Multidimensional arrays
Borrow a picture first:
The traditional way to merge a two-dimensional array into a one-dimensional array is to get all the array values and then assign them to a new array. For (var I =0; i<arr.length; i++) { for (var j=0; j<arr[i].length; J ++) {newarr.push (arr[I][j])}} // More multidimensional arrays can be used in the same wayCopy the code
Var newArr=[] arr.foreach (item => {newArr=[...newArr,...item]) var newArr=[] arr.foreach (item => {newArr=[...newArr,...item])Copy the code
for
Explore other array methods on your own! Related array methods:
The method name | describe |
---|---|
concat | Joins two or more arrays and returns the result |
every | Run the given function on each item in the array, returning true if the function returns true for each item |
filter | Returns an array of items that return true by running the given function on each item in the array |
forEach | Run the given function on each item in the array. This method returns no value |
join | Concatenate all array elements into a string |
indexOf | Returns the index of the first array element equal to the given argument, or -1 if not found |
lastIndexOf | Returns the maximum value in the index of the element searched in the array equal to the given parameter |
map | Returns an array of the results of each function call by running the given function on each item in the array |
reverse | Reversing the order of the elements in the array, the first element is now the last, and the last element is now the first |
slice | Pass in the index value and return the elements in the index range as the new array |
some | Run the given function on each item in the array, returning true if any item returns true |
sort | Sort arrays alphabetically, allowing you to pass in a function specifying the sorting method as an argument |
toString | Return an array as a string |
valueOf | Like toString, an array is returned as a string |