Javascript to learn
First, basic knowledge
Introduction to start
Language is introduced
Applicable scenario
Web end, server background, mobile phone APP, cross-platform desktop application
The development history
Learn to prepare
Open the console shortcut:
Mac: option + command + j
Run the process
The script to define
Two scripting methods
- Inline script
- External files
Note: scripts that introduce external files into the tag body do not execute, and the alert popover below will not execute
<script src="a.js">
alert('pop-up window');
</script>
Copy the code
To avoid delay
The script tag is best placed at the end of the body element to prevent blocking the rendering of the page
Code comments
Automatic semicolons
Variable declarations
Naming rules
Variable declarations
Weak type
Variable ascension
- Variables declared by var are promoted to the front
console.log(a); // undefined
var a = 1;
console.log(a); / / 1
Copy the code
- Var defined in if (false) will also be variable promoted, and if commented out will result in a different result
var web = 'bbb';
function hd() {
if (false) {
var web = 'bbb';
}
console.log(web);
}
hd(); // undefined
Copy the code
TDZ- Temporary dead zone
Temporary dead zone: A variable already exists in scope, but cannot be used until it is declared by let/const.
- Variables should be declared before they are used
- Let /const is recommended rather than var
The following code b is not allowed to use assignment directly without declaring it
function hd(a = b, b = 3) {}
hd(); // cannot access 'b' before initialization
Copy the code
Since a has been assigned, b can use the a variable, and the following code works fine
function hd(a = 2, b = a) {}
hd();
Copy the code
Block scope
Var/let/const in common
var
Var has no block-level scope and will pollute the world
for (var i = 0; i < 5; i++) {
console.log(i); // 0 1 2 3 4
}
console.log(i); / / 5
Copy the code
Not when let has block-level scope
for (let i = 0; i < 5; i++) {
console.log(i); // 0 1 2 3 4
}
console.log(i); // undefined
Copy the code
let
- Let has the block-scoped property that variables are only valid in the block domain
- Inside the block are variables that can be accessed to the upper scope
const
- Variables can only be declared once, and new assignments are not allowed
- However, you can change the value of a reference type variable
Object.freeze
If the variable is frozen, the variable cannot be modified, using strict mode will report an error
"use strict"
const obj = {
name: 'xiaoming'.age: 18
}
Object.freeze(obj);
obj.age = 19; // Error: cannot assign to read only property
console.log(obj.age); // Will not print
Copy the code
Value and address transmission
- Basic data types refer to simple data types such as values and strings, and reference types refer to object data types.
- Primitive type replication is a copy of values that does not affect each other.
- For reference types, variables hold Pointers to reference objects. Assignments between variables are actually Pointers to variables, so that multiple variables refer to the same object.
undefined
- Unassigned variables are undefined
- Function argument is undefined, function returns no value, return undefined
null
Strict mode
The basic difference
- Variables must be declared using keywords; undeclared variables are not allowed to be assigned
- Enforce declarations to prevent global contamination
- Keywords are not allowed to be used as variables
- Variable parameters are not allowed to be defined twice
- Strict mode can be set for functions separately
Deconstruction differences
- Non-strict mode can not use declaration directives, strict mode must use declarations.