JavaScript (basic)

Shenwei 2021-01-22 16:50:37
javascript basic


Focus on the front end of learning JavaScript Learning from , Because I haven't studied systematically before JavaScript, Therefore, the use of internship vacation time system of learning JavaScript From basic to advanced . Take notes at this time

# data type

JS There are six types of data in

  • String character string
  • Number The number
  • Boolean Boolean value
  • Null Null value
  • Undefined Undefined
  • Object object

# Basic data type

# String character string

  • JS You need quotation marks for strings in. Double quotation marks or single quotation marks are OK
  • Use... In a string \ As escape character
    • ' ==> '
    • " ==> "
    • \n ==> Line break
    • \t ==> tabs
    • \ ==> \
  • Use typeof Operator checking string , Returns the "string"

String related methods :

length

Gets the length of the string

charAt()

Gets the specified character according to the index

charCodeAt()

Gets the specified character code according to the index

fromCharCode()

Get the character according to the character code

indexOf(),lastIndexOf()

  • Retrieve the specified content from a string
  • You need a string as an argument , This string is what you want to retrieve , If the content is found , The index of its first occurrence is returned , Return if not found -1.
  • You can specify a second parameter , To indicate where to start searching
  • indexOf() It's from the front to the back
  • lastIndexOf() It's from the back to the front

slice()

  • You can intercept the specified content from a string , And return the intercepted content to , It doesn't affect the original variables
  • Parameters : first : Where the intercept starts ( Include start ) the second : Where the intercept ends ( Not including the end ) - You can omit the second parameter , If omitted, it will be intercepted to the end
    • You can pass negative numbers , If it's negative, count from back to front

substr()

and slice() Almost the same , The difference is that its second parameter is not index , It's the number of intercepts

substring()

and slice() Almost the same , The difference is that it can't take negative values as arguments , If you set a negative value , It will be automatically corrected to 0, substring() If the second parameter is less than the first , Automatic position adjustment

toLowerCase()

Converts a string to lowercase and returns

toUpperCase()

Converts a string to uppercase and returns

split()

You can split a string into an array according to the specified content Parameters :

  • You need a string as an argument , The array will be split according to the string
  • Can receive a regular expression , The array is split according to the regular expression

match()

You can extract what matches a string with a regular expression Parameters :

  • Regular expressions According to the regular expression, we can extract the content that meets the requirements from the string and encapsulate it into an array to return

replace()

You can replace the specified content in the string with the new content Parameters :

  1. first : What's being replaced , It can be a regular expression
  2. the second : New content to replace

search()

You can find the specified content in the string according to the regular expression Parameters :

  • Regular expressions The content will be queried based on the expression , And the index of the first matched content is returned , If nothing is matched , Then return to -1.

# Number The number

  • JS All integers and floating-point numbers in are Number type
  • Special numbers
    • Infinity It's just infinite
    • -Infinity Negative infinity
    • NaN Illegal number (Not A Number)
  • The representation of other base numbers :
    • 0b Start with binary , But not all browsers support
    • 0 Start with octal
    • 0x The beginning is hexadecimal
  • Use typeof Check one Number Type of data , Returns the "number"( Include NaN and Infinity)

# Number The number

  • Boolean values are mainly used for logical judgment , There are only two Boolean values
  • true The truth of logic
  • false The falsehood of logic
  • Use typeof When checking a Boolean value , Returns the "boolean"

# Null Null value

  • Null values are specifically used to represent empty objects ,Null There is only one value of type null
  • Use typeof Check one Null The value of type "object"

# Undefined Undefined

  • If you declare a variable but do not assign a value to it, then the value of the variable is undefined
  • There is only one value for this type undefined
  • Use typeof Check one Undefined Value of type , Returns the "undefined"

# Reference data type

# Object object

  • The object is JS Reference data types in
  • Object is a composite data type , Multiple attributes of different data types can be saved in the object
  • Use typeof When checking an object , Returns the object

Create objects

Mode one :

Mode two :

Add properties to the object

grammar : object . Property name = Property value ; object [" Property name "] = Property value ;

  • There is no requirement for the property name of the object , There is no need to follow the specification of identifiers , But in development , Try to write according to the identifier .
  • Property values can also be of any data type .

Read the properties in the object

grammar : object . Property name object [" Property name "] If you read a property that is not in an object , It doesn't report errors , It's back to a undefined

Delete attributes from objects

grammar : delete object . Property name delete object [" Property name "]

Use in Check whether the object contains the specified property

grammar : " Property name " in object

  • If the attribute is included in the object , Then return to true; If not, return false

Use object literal , Add properties directly to the object when you create it

grammar :

var obj = {
Property name : Property value ,
Property name : Property value ,
Property name : Property value ,
Property name : Property value
}

1 2 3 4 5 6

Copy

# The difference between basic data type and reference data type

  • Basic data type String Number Boolean Null Undefined
  • Reference data type Object
  • Basic data type data , Variables are stored directly, and their values . Variables are independent of each other , Modifying one variable does not affect the other variables .
  • Reference data type data , A variable is a reference to a saved object ( Memory address ). If multiple variables point to the same object , At this point, modify the properties of a variable , It affects other variables .
  • When you compare two variables , For basic data types , The comparison is the value , For reference data types, the comparison is address , The same address is the same

# Type conversion

Type conversion refers to the conversion of other data types , Convert to String Number or Boolean

# Convert to String

# Mode one ( Cast ):

  • Call the... Of the converted data toString() Method
  • Example :
var a = 123;
a = a.toString();

1 2

Copy

  • Be careful : This method does not apply to null and undefined Because there are no methods in these two types of data , So call toString() When an error

# Mode two ( Cast ):

  • call String() function
  • Example :
var a = 123;
a = String(a);

1 2

Copy

  • principle : about Number Boolean String Will call them toString() Method to convert it to a string , about null value , Convert directly to a string "null". about undefined Convert directly to a string "undefined"

# Mode three ( Implicit type conversion ):

  • For any data type +""
  • Example :
var a = true;
a = a + "";

1 2

Copy

  • principle : and String() The function is the same

# Convert to Number

# Mode one ( Cast ):

  • call Number() function
  • Example :
var s = "123";
s = Number(s);

1 2

Copy

  • The conversion situation :
    1. character string --> Numbers
      • If the string is a legal number , Then it is directly converted to the corresponding number
      • If the string is an illegal number , Convert to NaN
      • If it's an empty string or a pure space string , Convert to 0
    2. Boolean value --> Numbers
      • true Convert to 1
      • false Convert to 0
    3. Null value --> Numbers
      • null Convert to 0
    4. Undefined --> Numbers
      • undefined Convert to NaN

# Mode two ( Cast ):

call parseInt() or parseFloat()

These two functions are specifically used to convert a string to a number

parseInt()

  • You can extract valid integer bits from a string , And converted to Number
  • Example :
var a = "123.456px";
a = parseInt(a); //123

1 2

Copy

parseFloat()

  • You can extract valid decimal places from a string , And converted to Number
  • Example :
var a = "123.456px";
a = parseFloat(a); //123.456

1 2

Copy

# Mode three ( Implicit type conversion ):

  • Use one dollar + For implicit type conversion
  • Example :
var a = "123";
a = +a;

1 2

Copy

  • principle : and Number() The function is the same

# Convert to Boolean

# Mode one ( Cast ):

  • Use Boolean() function
  • Example :
 var s = "false";
s = Boolean(s); //true

1 2

Copy

  • The conversion situation
    1. character string --> Boolean
      • Except for empty strings, the rest are true
    2. The number --> Boolean
      • except 0 and NaN The rest are all true
    3. null、undefined ---> Boolean
      • All are false
    4. object ---> Boolean
      • All are true

# Mode two ( Implicit type conversion ):

  • Do two non operations for any data type , You can convert it to a Boolean value
  • Example :
var a = "hello";
a = !!a; //true

1 2

Copy


# Operator

  • Operators are also called operators
  • Operators allow you to operate on one or more values

# typeof Operator

  • The data type used to check a variable
  • grammar :typeof Variable
  • It returns a string describing the type as the result

# Arithmetic operator

  • + Add two values and return the result
  • - Subtract two values and return the result
  • * Multiply two values and return the result
  • / Divide two values and return the result
  • % Carry out the remainder operation on two values and return the result

  • Besides addition , Right Number When the value of type is evaluated , Will be converted to Number And then doing the arithmetic .
  • And in addition , If you add two strings , Will do string operation , Concatenate two characters into a string .
  • Add any value and string , Will be converted to a string first , And then put it together

# Unary operator

Unary operators only need one operand

One dollar +

  • It's a plus sign , There is no effect on the value , But you can convert a non number to a number
  • Example :
var a = true;
a = +a;

1 2

Copy

One dollar -

  • It's a minus sign , You can reverse the sign bit of a number
  • Example :
var a = 10;
a = -a;

1 2

Copy

Self increasing

  • Self increasing can make the variable self increasing on the basis of the original value 1
  • Self increasing use ++
  • Autoincrement can be used front ++(++a) after ++(a++)
  • Whether it's ++a still a++ Will immediately increase the original variable 1, The difference is ++a and a++ The values are different :
    • ++a The value of is the new value of the variable ( The value after self increase )
    • a++ The value of is the original value of the variable ( Value before autoincrement )

Self reduction

  • Self subtraction can make the variable self subtraction on the basis of the original value 1
  • Reduce the use of --
  • Self subtraction can be used front --(--a) after --(a--)
  • Whether it's --a still a-- Will immediately reduce the original variable 1, The difference is --a and a-- The values are different :
    • --a The value of is the new value of the variable ( The value after subtraction )
    • a-- The value of is the original value of the variable ( The value before subtraction )

# Logical operators

  • !
    • A non operation can negate a Boolean value ,true change false false edge true
    • When using... For non Boolean values ! when , It will be converted to a Boolean and then negated
    • We can use ! To convert other data types to Boolean values
  • &&
    • && The values on both sides of the symbol can be calculated
    • Only the values at both ends are zero true when , Will return true. As long as there is one false It will return false.
    • And is a short-circuit and , If the first value is false, The second value is no longer checked
    • For non Boolean values , It's going to convert it to a Boolean and do the operation , And return the original value
    • The rules : 1. If the first value is false, Returns the first value 2. If the first value is true, Then return the second value
  • ||
    • || You can do or operate on the values on both sides of the symbol
    • Only at both ends false when , Will return false. As long as there is one true, It will return true.
    • Or a short circuit or , If the first value is true, The second value is no longer checked
    • For non Boolean values , It's going to convert it to a Boolean and do the operation , And return the original value
    • The rules : 1. If the first value is true, Returns the first value 2. If the first value is false, Then return the second value

# Assignment operator

  • =
    • You can assign the value on the right side of the symbol to the variable on the left
  • +=
    • a += 5 amount to a = a+5
    • var str = "hello"; str += "world";
  • -=
    • a -= 5 amount to a = a-5
  • *=
    • a *= 5 amount to a = a*5
  • /=
    • a /= 5 amount to a = a/5
  • %=
    • a %= 5 amount to a = a%5

# Relational operator

  • The relational operator is used to compare the size relationship between two values
    • >
    • >=
    • <
    • <=
  • The rules of relational operators are the same as in mathematics , Used to compare the relationship between two values , If the relationship holds, return true, If the relationship is not established, return false.
  • If the two values being compared are non numeric , It will be converted to Number Then compare .
  • If the two values being compared are strings , The string will be compared Unicode code , It doesn't translate into Number.

# Equality operator

  • ==
    • equal , Judge whether the left and right values are equal , If equal returns true, If you don't wait to return false
    • Equality automatically casts two values , If you compare different types , It will be converted to the same type and then compared , After conversion, it also returns true
  • !=
    • Unequal , Judge whether the left and right values are not equal , If not, return to true, If equal, return false
    • You can also do automatic type conversion .
  • ===
    • Congruence , Judge whether the left and right values are congruent , It's similar to equality , It's just that it doesn't do automatic type conversion , If the two values are of different types , Then return directly false
  • !==
    • Incongruence , It's similar to inequality , But it doesn't do automatic type conversion , If the two values are of different types , It will go straight back to true
  • Special value :
    • null and undefined
      • because undefined Derived from null, therefore null == undefined Returns the true. however null === undefined Returns the false.
    • NaN
      • NaN Not equal to any value , Report itself NaN == NaN //false
    • Judge whether a value is NaN
      • Use isNaN() function

# Ternary operator

  • ? :
    • grammar : Conditional expression ? sentence 1: sentence 2;
    • Execute the process : First, evaluate and judge the conditional expression , If the result is true, Then execute the statement 1, And return the execution result If the result is false, Then execute the statement 2, And return the execution result

# priority

  • As in Mathematics ,JS Operators in are also prioritized , such as First, Add or subtract after With the first After or
  • The specific priority can refer to the priority table , The higher the priority is in the table , The higher the priority, the higher the priority , Same priority , Count from left to right .
  • Priorities don't need to be remembered , If the more uncertain , Use () To change priorities .

# Flow control statement

  • Programs are executed from top to bottom , The order of program execution can be changed by flow control statement , Or repeatedly execute a certain program .
  • classification :
    1. Conditional statements
    2. Conditional branch statement
    3. Loop statement

# Conditional statements

Conditional statements are also called if sentence

# Grammar 1 :

if( Conditional expression ){
sentence ...
}

1 2 3

Copy

  • Execute the process : if Statement execution time , Will evaluate the conditional expression first , If the value is true, execute if The following sentence If the value is false, Do not perform

# Grammar II :

if( Conditional expression ){
sentence ...
}else{
sentence ...
}

1 2 3 4 5

Copy

  • Execute the process : if...else Statement execution time , Will evaluate the conditional expression , If the value is true, execute if The following sentence If the value is false, execute else The following sentence

# Grammar 3 :

if( Conditional expression ){
sentence ...
}else if( Conditional expression ){
sentence ...
}else if( Conditional expression ){
sentence ...
}else if( Conditional expression ){
sentence ...
}else{
sentence ...
}

1 2 3 4 5 6 7 8 9 10 11

Copy

  • Execute the process if...else if...else Statement execution time , Will evaluate the conditional expression from top to bottom , If the result is true, Then execute the current if The following sentence , After execution, the statement ends . If the result is false, Then continue to judge down , Until it's found for true Until . If all the conditional expressions are false, execute else The following sentence

# Conditional branch statement

switch sentence

# grammar :

switch( Conditional expression ){
case expression :
sentence ...
break;
case expression :
sentence ...
break;
case expression :
sentence ...
break;
default:
sentence ...
break;
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Copy

  • Execute the process : switch...case... Statement at execution time , Will in turn case The value of the following expression and switch After the expression of the value of congruent comparison , If the comparison is false, Then continue to compare down . If the comparison is true, From the present case Start executing code down at . If all case The judgment is that false, From default Start executing code at .

# Loop statement

Some statements can be executed many times through loop statements

# while loop

  • grammar :
while( Conditional expression ){
sentence ...
}

1 2 3

Copy

  • Execute the process : while Statement at execution time , Will evaluate the conditional expression first , If the result is false, Then stop the cycle If the result is true, Then execute the loop body Loop body execution completed , Continue to evaluate the conditional expression , And so on

# do...while loop

  • grammar :
do{
sentence ...
}while( Conditional expression )

1 2 3

Copy

  • Execute the process do...while When executed , Will execute first do After the cycle body , And then judge the conditional expression , If the result is false, Then stop the cycle . If the result is true, Then continue to execute the loop body , And so on
  • and while The difference between :
    • while: Judge before you execute
    • do...while: Execute before judge
    • do...while You can make sure that the loop body is executed at least once .

# for loop

  • grammar :
for(① Initialization expression ; ② Conditional expression ; ④ Update expression ){
③ sentence ...
}

1 2 3

Copy

  • Execute the process : First, execute ① Initialization expression , Initialize a variable , Then on ② Conditional expressions are evaluated , If false Then stop the cycle If the result is true, execute ③ The loop body Loop body execution completed , perform ④ Update expression , Update variables . After the update expression is executed, repeat ②

# Dead cycle

while(true){
}
for(;;){
}

1 2 3 4 5 6 7

Copy


# function (Function)

  • A function is also an object , It also has the function of ordinary objects
  • Functions can encapsulate some code , You can call the function to execute the code when you need it
  • Use typeof Checking a function returns function

# Create a function

# Function declaration

function Function name ([ Shape parameter 1, Shape parameter 2... Shape parameter N]){
sentence ...
}

1 2 3

Copy

# Function expression

var Function name = function([ Shape parameter 1, Shape parameter 2... Shape parameter N]){
sentence ...
};

1 2 3

Copy

# Call function

  • grammar : Function object ([ Actual parameters 1, Actual parameters 2... Actual parameters N]); fun() sum() alert() Number() parseInt()
  • When we call a function , The code encapsulated in the function is executed in the order it was written

# Formal parameters and actual parameters

  • Shape parameter : Formal parameters
    • When defining a function , Can be in () Define one or more formal parameters in , Use between formal parameters , separate Defining formal parameters is equivalent to declaring the corresponding variable in the function but not assigning values , Parameters are assigned only when called .
  • Actual parameters : The actual parameter
    • When you call a function , Can be in () Transfer argument , The passed argument is assigned to the corresponding formal parameter , When you call a function JS The parser doesn't check the type and number of arguments , You can pass values of any data type . If the number of arguments is greater than the number of formal parameters , Extra arguments will not be assigned , If the number of arguments is less than the number of formal arguments , The formal parameters without corresponding arguments will be assigned values undefined

# Return value , It's the result of function execution .

  • Use return To set the return value of the function .
  • grammar : return value ; This value becomes the return value of the function , You can receive the return value through a variable
  • return The following code will not execute , Once implemented to return When the sentence is , The function will exit immediately .
  • return It can be followed by any type of value , It can be a basic data type , It can also be an object .
  • If return After not following the value , Or not return By default, the function returns undefined.
  • break、continue and return
    • break Exit loop
    • continue Skip this cycle
    • return Exit function

# Parameters

  • Function arguments can also be of any data type .

# Method (method)

  • You can set a function as a property of an object , When the property of an object is a function , We call this function the method of the object .
  • object . Method name ();
  • Function name ();

# call() and apply()

  • These two methods are methods of function objects, which need to be called through function objects
  • There are two ways to call functions directly , And you can use the first argument to specify this
  • The difference is call Is an argument to a direct transfer function and apply You need to encapsulate the arguments into an array and pass them

# arguments

  • arguments and this similar , They are all implicit parameters in functions
  • arguments Is a class array element , It is used to encapsulate the arguments in the execution of a function So even if you don't define formal parameters , It can also be done through arguments To use arguments
  • arguments There is a property in callee Represents the currently executed function object

# Scope

Scope is simply the scope of a variable .

stay JS There are two types of scopes in

# Global scope

  • Directly in script The code written in the tag runs in the global scope
  • The global scope is created when the page opens , Destroy when the page is closed .
  • There is a global object in the global scope window,window The object is provided by the browser , You can use it directly on the page , It represents the entire browser window .
  • Variables created in the global scope are treated as window Object's properties are saved All functions created in the global scope are treated as window Object method save
  • Variables and functions created in the global scope can be accessed anywhere on the page . Variables in the global scope can also be accessed in the function scope .
  • Try not to create variables in the global

# Function scope

  • Function scope is the scope created when a function is executed , Each time the function is called, a new function scope is created .
  • Function scopes are created at function execution time , Destroy at the end of function execution .
  • Variables created in function scope , Cannot access in the global .
  • When you use a variable in the scope of a function , It will first look for , If found, use , If not, go to the next level of scope to find , If found, use , If not, keep looking up , Always will

# Variable declaration ahead of time

  • In global scope , Use var Keyword declared variables are declared before all code is executed , But no assignment . So we can use variables before declaring them . But don't use var Variables declared by keywords are not declared ahead of time .
  • In the scope of a function , It also has this characteristic , Use var The variables declared by the keyword are declared before all the code of the function is executed , If not used var Keyword declaration variables , Then the variable becomes a global variable

# Function declaration ahead of time

  • In global scope , Functions created using function declarations (function fun(){}), Will be created before all code is executed , That is, we can call the function before the function declaration , But using function expressions (var fun = function(){}) The created function does not have this feature
  • In the scope of a function , Functions created using function declarations , It will be created before the code in all functions is executed .

# this( Context object )

  • Every time we call a function , The parser passes a context object as an implicit parameter to the function . Use this To reference context objects , Depending on the call form of the function ,this The value of is different .
  • this In different situations :
    1. When called as a function ,this yes window
    2. When called as a method ,this It's the object that calls the method
    3. When called as a constructor ,this It's the new object
    4. Use call and apply Invocation time ,this It's the designated object
    5. In global scope this representative window

# Constructors

  • Constructors are functions that are specifically used to create objects
  • A constructor can also be called a class
  • Objects created through a constructor , We call this object an instance of this constructor
  • Objects created by the same constructor , We call it a kind of object
  • Constructor is a normal function , It's just that he calls it in a different way , If called directly , It's just a normal function If you use new To call , Then it's a constructor
  • Example :
function Person(){
}

1 2 3

Copy

Constructor execution flow :

  1. Create a new object
  2. Take the new object as the context object of the function (this)
  3. Execute the code in the function
  4. Return the new object to

instanceof Used to check whether an object is an instance of a class

  • grammar :
 object instanceof Constructors 

1

Copy

Enumerate properties in objects for...in grammar :

for(var Property name in object ){
}

1 2 3

Copy


# Prototype (prototype)

  • After creating a function , The parser adds a number to the function by default prototype prototype Property points to an object , This object is what we call a prototype object .
  • When a function is used as a constructor , It creates objects that have an implicit property that executes the prototype object . This implied property can be accessed through the object .__proto__ To visit .
  • Prototype object is equivalent to a public area , All objects created by the same constructor usually have access to the same prototype object . We can add common properties and methods to the prototype object , So we only need to add it once , So that all objects can be used .
  • When we access a property of an object or call a method of an object , It will first find in itself , If you find it in yourself , Direct use , If not found , Then go to the prototype object to find , If found, use , If not found , Then go to the prototype of the prototype to find , And so on . Until I find Object Until the prototype of ,Object The prototype of is null, If it is still not found, return to undefined
  • hasOwnProperty()
    • This method can be used to check whether the object itself contains an attribute
    • grammar : object .hasOwnProperty(" Property name ")

# Array (Array)

  • An array is also an object , It's an object used to store data and Object similar , But its storage efficiency is higher than that of ordinary objects
  • The contents of an array are called elements
  • Arrays use indexes (index) To manipulate elements
  • Index refers to 0 The starting integer

# Array operation

# Create array

var arr = new Array();
var arr = [];

1 2

Copy

# Adding elements to an array

 Array objects [ Indexes ] = value ;
arr[0] = 123;
arr[1] = "hello";

1 2 3

Copy

# Add elements directly when creating an array

var arr = [ Elements 1, Elements 2.... Elements N];
// Example :
var arr = [123,"hello",true,null];

1 2 3

Copy

# Get and modify the length of the array

  • Use length Property to manipulate the length of an array
  • To obtain the length of the : Array .length
    • length Get the maximum index of the array +1
    • For continuous arrays ,length What we get is the number of elements in the array
  • Modify the length of an array Array .length = New length
    • If the revised length Larger than the original length , The extra part will be empty
    • If the revised length Less than the original length , The extra elements in the original array will be deleted
  • Add elements to the end of the array Array [ Array .length] = value ;

# Array method

# push()- Add element at the end of the array

  • Used to add one or more elements to the end of an array , And return the new length of the array
  • grammar : Array .push( Elements 1, Elements 2, Elements N)

# pop()- Delete the last element of the array

  • To delete the last element of an array , And return the deleted element

# unshift()- Add element before array

  • Add one or more elements to the front of the array , And returns the new length of the array

# shift()- Delete the element before the array

  • Delete an element in front of the array , And return the deleted element

# slice()- Intercept array

  • You can intercept the specified elements from an array
  • This method does not affect the original array , Instead, encapsulate the intercepted content into a new array and return
  • Parameters : 1. Index at the start of the interception ( Including start position ) 2. Intercept the index of the end position ( Does not include end position ) - The second parameter can be omitted , If you don't write it, it will be intercepted until the end
    • Parameter can pass a negative value , If it's negative , Then count from back to front

# splice()- Delete the specified element and replace

  • Can be used to delete the specified elements in the array , And replace... With new elements This method encapsulates the deleted elements into a new array and returns
  • Parameters : 1. Delete the index at the start 2. Number of deletions 3. After three , They're all replacement elements , These elements will be inserted in front of the start position index

# reverse()- Inversion array

  • Can be used to invert an array , It affects the original array

# concat()- Mosaic array

  • You can connect two or more arrays , It doesn't affect the original array , Instead, the new array is returned as a return value

# join()- Array to string

  • You can convert an array to a string
  • Parameters : You need a string as an argument , This string will be used as a connector to connect the elements in the array If you do not specify a connector, the default is ,

# sort()- Array sorting

  • You can sort the contents of an array , The default is Unicode Code to sort After calling , Will directly modify the original array .
  • You can specify your own sort rules , You need a callback function as an argument :
function(a,b){
// Ascending order
//return a-b;
// Descending order
return b-a;
}

1 2 3 4 5 6 7 8

Copy

# Traversal array

  • Traversing an array is to get all the elements in the array to
  • Generally, we use it for Loop through the array :
for(var i=0 ; i< Array .length ; i++){
// Array [i]
}

1 2 3

Copy

  • Use forEach() Method to traverse the array ( Are not compatible IE8)
 Array .forEach(function(value , index , obj){
});

1 2 3

Copy

forEach() Method requires a callback function as an argument , There are several elements in the array , The callback function will be called several times , On every call , Will traverse to the information in the form of arguments passed in , We can define formal parameters to get this information .

  • value: The element being traversed
  • index: Traversing index of element
  • obj: Traversed object

# Packaging

stay JS Three packaging classes are provided for us in : String() Boolean() Number() These three wrapper classes allow you to create objects of basic data types Example :

var num = new Number(2);
var str = new String("hello");
var bool = new Boolean(true);

1 2 3

Copy

When we manipulate the properties and methods of a basic data type , The parser will temporarily convert it to the corresponding wrapper class , And then to manipulate properties and methods , After the operation, destroy the temporary object .

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Shenwei]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210122164410686L.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云