Original link: https://yzyyz.top/archives/start_with_JS.html
Introductory stuff, nothing fancy
variable
var
var
declares a variable with global or function level scope
1 |
var a; |
let
let
declares a block-scoped variable
1 |
// declare the same as var |
Variables declared in block-level scope cannot be accessed outside
1 |
{ |
Can’t reset with let
in the same scope
1 |
let a = 1 ; |
const
const
declares one or more constants at the block-level scope, which must be initialized when declaring, and the value cannot be modified after initialization, and the variable cannot be reset like let
1 |
const a = 666 ; |
But in fact, the object declared by const
can be modified
1 |
const a = [ 1 , 2 , 3 ] |
variable promotion
Both function and variable declarations will be hoisted to the front of the function
1 |
a = 0 ; |
Functions created in the form of function declarations will be created before execution
function expressions are not advanced
1 |
fun(); |
type of data
typeof
is used to check the type
String
Number
1 |
console .log( Number .MAX_VALUE) // The maximum value that js can represent, if it is greater than it returns Infinity |
Infinity
is infinity
-Infinity
negative infinity
NaN
not a number
Floating-point arithmetic results may be imprecise
Boolean
Null
null
empty object
Undefined
undefined undefined
Object
type conversion
1 |
var a = 123 ; |
1 |
var a = "123" ; |
0
, NaN
, ""
, null
, undefined
are converted to false
1 |
var a = 0 ; |
other
Hexadecimal: 0x10
Octal: 010, some browsers treat it as decimal
Binary: 0b10, poor compatibility
Unicode: \u100 (Use ⚀ in web pages)
operator
arithmetic operators
+
-
*
/
%
unary operator
-a
+a
self-increment
a++
original value, ++a
new value
Logical Operators
!
No
1 |
var a = true ; |
&&
with
||
or
assignment operator
a += 1;
…
relational operator
Comparing string numbers must be converted
equality operator
==
===
conditional operator
条件表达式?语句1:语句2;
true executes 1, false executes 2
operator precedence
…
Loop judgment
if
1 |
if (){ |
switch
1 |
if (a == 1 ){ |
while
1 |
while (){ |
for
1 |
for ( var a= 1 ; a< 10 ; a++){ |
Object
built-in objects
Objects defined by the ES standard
function
Math
String
…….
host object
Browser-provided objects
Objects in BOM and DOM
window
Common properties
-
document
document object
-
console
-
location
Address object, read web links or control jumps
-
localStorage
local storage
-
screen
get screen resolution
-
scrollX, scrollY
X, Y scroll bar position
Common method
-
alert
-
confirm
-
blur
Move focus out of current window
-
close
close the window
-
eval
Execute a js statement in the form of a string
-
focus
Activate the current window
-
open
Open new window (tab)
-
openDialog
Open new dialog window
-
print
Dialog box showing print function
-
prompt
Pop up the input box (will block js)
-
scroll
Control the scroll bar to the specified position
-
stop
stop loading
custom object
1 |
// The new keyword calls the constructor constructor |
1 |
// object literal |
function
function
1 |
var fun = new Function ( "console.log('6666')" ) // not commonly used |
method
1 |
var fun = function ( ) { |
scope
global scope
The code written directly in js is created when the page is opened and destroyed when it is closed;
There is an object window
in the global scope, and the created variables are saved as properties of window
; the created functions are saved as methods of window
;
Variables in the global scope are global variables
1 |
var a = 123 ; |
function scope
Created when the function is called, destroyed after execution, and created for each call, independent of each other;
Variables in the global scope cannot be accessed;
When manipulating a variable in the function scope, first look for it in its own scope, if not, look for the upper level, until the global scope, if not found, a ReferenceError
Implicit parameter
this
An implicit parameter passed by the parser to the internal when calling a function, this
points to different objects according to the method of the function call;
-
Call it in the form of a function that points to
window
-
Called as a method, it points to the object
-
Called as a constructor, pointing to the newly created object
-
When using
call()
andapply()
, point to the specified object
1 |
function fun ( ) { |
call() and apply()
Both call()
and apply()
are methods of function objects, and functions are executed when called.
When calling, you can specify an object as the first parameter, and the object will become this
when the function is executed.
1 |
var a = { name : "Apricot Flower" }; |
call()
and apply()
pass parameters differently
1 |
function fun ( a,b ) { |
arguments
It is also an implicit parameter passed in, which is an array-like object
- The arguments passed by the calling function are stored in
arguments
,arguments.length
can be used to get the length of the arguments - Even if you don’t define formal parameters, you can use it to get
1 |
function fun ( ) { |
-
arguments
have acallee
property, which is the currently executing function
1 |
function fun ( ) { |
factory function
1 |
function createSth ( name ) { |
Constructor
The constructor is created with the new
keyword
-
will immediately create a new object
-
Set the newly created object as
this
in the function -
execute code
-
return object
1 |
function Preson ( name ) { |
Look at the code below
1 |
function Person ( name ) { |
If you construct n objects with this constructor, n new run
methods are created, which is unnecessary:
1 |
function Person ( name ) { |
However, the above definition of functions in the global scope will pollute the namespace of the global scope, and it is not safe. This means the prototype object
prototype object prototype
For each function created, the parser adds a prototype
property to it, accessed via __proto__
1 |
function MyClass ( ) { |
The prototype of an Object object has no prototype
toString()
When console.log()
prints an object, it actually outputs the return value of the object’s toString()
1 |
function P ( ) { |
garbage collection
When an object does not have any variable or property reference to it, it can never be manipulated at this time, it is garbage;
JS has an automatic garbage collection mechanism that will automatically destroy it, we just need to set its reference to null
arrayArray
1 |
var arr = new Array (); |
Methods JavaScript Array Reference Manual (w3school.com.cn)
timeDate
1 |
var d = new Date (); // Created directly with the constructor, it will encapsulate the current code execution time |
JavaScript Date Reference Manual (w3school.com.cn)
Math
It is not a constructor, it belongs to a utility class, no need to create
1 |
console.log ( Math.PI ); |
JavaScript Math Reference Manual (w3school.com.cn)
1 |
console .log( Math .ceil( 1.4 )); // round up |
Packaging class
Three wrapper classes are provided in JS, through which basic data types can be converted to objects:
- String()
- Number()
- Boolean()
1 |
var num = new Number ( 1 ); |
There is basically no packaging class in development
When properties and methods are called on values of primitive data types, the browser temporarily uses the wrapper class to convert them to objects before calling the properties and methods
After calling, convert to basic data type
1 |
var a = 132 ; |
String String
some methods
Store the underlying string as an array [“H”, “e”, ….]
JavaScript String Reference Manual (w3school.com.cn)
1 |
var str = "Hello YZYYZ" ; |
regular expression
1 |
var re = new RegExp ( "a" ); |
Methods that support regular String objects
1 |
var str = "123qqq345www456eee567ABCDEFG" ; |
DOM
Document Object Model
JS operates on HTML documents through DOM
- Documentation: HTML web page
- Objects: each part of the web page becomes an object
- Model: Represents the relationship between objects
Node Node
A node is the most basic part of a web page, and each part of a web page can be called a node
nodeName | nodeType | nodeValue | |
---|---|---|---|
document node | #document | 9 | null |
element node | label name | 1 | null |
property node | property name | 2 | attribute value |
text node | #text | 3 | text content |
The browser provides the document node object, which is the window
property and can be used directly on the page
1 |
... |
event
HTML DOM Events (w3school.com.cn)
- addEventListener Add event listener
- dispatchEvent triggers the specified event manually
- getAttribute Gets the specified attribute of the label
1 |
function fun ( ) { |
document loading
1 |
... |
DOM query
get element node
- getElementById()
- getElementsByTagName()
- getElementsByName()
get child node
-
childNodes
Text properties are also treated as nodes
-
children
recommend
-
firstChild
-
firstElementChild
The first child element is IE dead, but it is only compatible with IE9+
-
lastChild
Get parent and sibling nodes
-
parentNode
parent node
-
previousSibling
previous sibling
-
nextSibling
According to class
-
getElementsByClassName
Include all elements of itself
Selector
-
document.querySelector()
only find the first
-
document.querySelectorAll()
return array
DOM additions, deletions and modifications
HTML DOM Element Object (w3school.com.cn)
-
document.createElement()
Takes a tag name as a parameter and returns this object
-
document.createTextNode()
Create a text node, the parameter is text, and return the node
-
appendChild()
parent element. appendChild(child element)
-
insertBefore()
parent node.insertBefore(new node, old node)
-
replaceChild()
replaceChild(new node, old node) -
removeChild()
parent node. removeChild(child node)
parent node: child node. parentNode
This article is reprinted from: https://yzyyz.top/archives/start_with_JS.html
This site is for inclusion only, and the copyright belongs to the original author.