JS Summary of basic knowledge
JS Attention of code introduction :
* In a pair script There is an error in the tag js code , So, the js It will not be implemented
* If the first pair script In the label js There is an error in the code , But it won't affect the next couple script In the label js Code execution .
* When html follow h5 Version of ,script In the label type=“text/JavaScript” perhaps language=”javascript”, It can be omitted .
* Html Multiple pairs can appear in the file script label .
* Script The label is usually placed in the body Last line in label , Sometimes it appears in the body in , as vue.
* If script Tag pass src Introducing external js file , So don't write code in this pair of tags .
JS Raw data type in :number,string,boolean,null,undefined,object
* A variable declaration , But not assigned , The output is undefined
* Function has no explicit return value , If you accept with a variable , The output is undefined
* Undefine + number = NaN
How to get the type of variable ?
use typeof( variable ) perhaps typeof variable (typeof It's a keyword )
results of enforcement :
Number Range of type numbers
in addition ,js Floating point operations in , Inaccurate accuracy . Don't compare decimals with decimals , There may be problems .
NaN Not equal to any number , Including itself . You need to determine whether a value is NaN, use isNaN()
-----------------------------------------------------------
-------------------------------------------------------------
isNaN(): The number passed in is not true , Is the number false .
Operation of string
If you want the value of a variable to be null, You have to assign values directly null, There is no other way .
Type conversion
* Other types to digital :
----------------------------------------------
----------------------------------------------
* Other types to string types .
toString No turning null,undefined Such values , There will be an error report
result :
----------------------------------------------
result :
3. Other types to Boolean .
----------------------------------------------
== Only compare values for equality , Do not compare types .
=== Compare whether the values and types are equal .
Definition of array :
* Create an array through the constructor
result :[]
----------------------------------------------
result :
----------------------------------------------
* Create an array by literal quantity
result :[]
----------------------------------------------
Array can store various data types :
Traversal array
ForEach() The first parameter of the callback function in is the element , The second parameter is the index
Definition of function
Function foo(){},
If you define a function with js Built in function duplicate name , The system functions are covered
The browser will not pop up .
----------------------------------------------
The number of formal and actual parameters in a function can be inconsistent .
----------------------------------------------
When the function has no return value , But it uses a variable to receive , The value of this variable is undefined
----------------------------------------------
When the function has no explicit return value , The variable that receives the return value of the function is undefined
----------------------------------------------
The list of arguments in a function is in the form of an array , But this array is a pseudo array .
result :
----------------------------------------------
Other ways to define functions :
Normal function declaration
----------------------------------------------
Function expression : Assign an anonymous function to a variable
A semicolon is required after the function expression
----------------------------------------------
If the function declaration is overridden , No matter where the function is called , They call newly declared functions .
----------------------------------------------
If the function expression is overridden , The function is called to execute the currently declared function
----------------------------------------------
How to call anonymous functions ?
Called at the time of declaration , It can only be executed once .
----------------------------------------------
How to get the data type of a function ?
----------------------------------------------
Functions are used as arguments
A function is transmitted as a parameter , This function is called a callback function .
-----------------------------------------------
Function as the return value .
----------------------------------------------
Scope
use var Declared variables , There is no block level scope , Use as global variable , Too many global variables , Untimely release , It takes up memory .
----------------------------------------------
Only variables defined within a function , There are local variables , External inaccessible .
------------------------------------------------
Declaration of implicit global variables
result :
----------------------------------------------
Implicit global variables can be deleted
----------------------------------------------
Scope chain
result :30
The variable used is found in the current scope , If the variable does not exist in the current scope , Then look up , If the variable is not found up to the global scope , The value of the variable is undefined
----------------------------------------------
Pre analysis
When you don't declare a variable or a function is not declared , Use variables or functions , Pre resolution will be generated , Declare the variables and the top functions in advance .
result :
----------------------------------------------
Preanalytic piecewise problem
result :
----------------------------------------------
result :
Many pairs script Duplicate names of functions in Tags , Pre resolution does not conflict .
----------------------------------------------
result :
----------------------------------------------
result :
----------------------------------------------
----------------------------------------------
create object
* Call the system constructor to create the object
Inside the object method , use this Access the properties of an object ,instanceof judge person Is it object example .
---------------------------------------------------
How to create multiple objects at once ?
Encapsulate the code that creates the object in a function .
----------------------------------------------
So every time you create an object , The properties are the same . By passing parameters , Changing the basic properties of an object , This approach is called factory pattern creation objects .
----------------------------------------------
* Custom constructor , create object .
In the same way , If you need to create multiple objects , How you can pass parameters to a function .
----------------------------------------------
When creating an object with a custom constructor , What happened? ?
* Request memory space , Store new objects created
* hold this Set to current object
* Set the property and method values of the object .
* hold this This object returns .
Object that is saved on the heap when the object is created , An instance is a reference to the address of the current object stored in the stack .
* Create an object in a literal way
The literal created object is a one-time object .
Another form of accessing the properties of an object , adopt [ Property name ], Index to a property .
----------------------------------------------
JSON object
Json Objects, whether they are keys , Or are values in double quotation marks .
----------------------------------------------
Json Data traversal , out of commission for loop , But it can be used for-in
----------------------------------------------
Basic data type ( Value type ):number,string,boolean
Complex type ( reference type ):object
Null type :undefined,null
----------------------------------------------
Where are values of value types stored ? The stack stores specific values
Where are the values of reference types stored ? Stack address and code block in stack
Value type assignment passes a value .
Reference type assignment , It's an address .
----------------------------------------------
static object : object ={}. name = function(){}, Objects created in this form , Static object .
JS Three objects in :
* Built in objects
* custom object
* Browser object
------------------------------------------------------
Built in objects :Math,Date,String,Array,Object
Built in objects Math Common methods of
Math.PI
Math.abs()
Math.ceil()
Math.floor()
Math.max()
Math.min()
Math.pow()
Math.sqrt()
Math.random()
----------------------------------------------
Built in objects Date Common methods of
----------------------------------------------
----------------------------------------------
String object
Str value hello Stored on a stack
S The address is stored in , point new String Application address ,new String(‘hello’) Stored on a heap .
----------------------------------------------
String properties : Immutability
----------------------------------------------
-----------------------------------------------------
Common methods of string
,
result :
----------------------------------------------
result :
----------------------------------------------
result :
----------------------------------------------
result :
----------------------------------------------
result :
----------------------------------------------
result :,
----------------------------------------------
result :
----------------------------------------------
result :
----------------------------------------------
result :
----------------------------------------------
result :
Technology