JS
In thelet
,const
,var
1.JS
How many ways are there to define variables in?
let
const
var
class
import
function
2,let
,const
,var
What’s the difference?
var |
let |
const |
---|---|---|
There is no block-level scope | There are block-level scopes | There are block-level scopes |
Declare global variables inwindow 下 |
Global variables are not under global attributes | Global variables are not under global attributes |
Redefining a variable does not report an error | complains | complains |
Declare a variable | Declare a variable | Declare a constant |
There is variable promotion | There is no variable promotion | There is no variable promotion |
Assign at any time after the declaration | Assign at any time after the declaration | Assign immediately after the declaration |
3,const
Can I change a defined constant?
const
Defining base types cannot be modified;const
Defining a reference type allows you to modify the value inside the reference type.
If I want toconst
Defining a reference type does not change its value.
Object.freeze
;- Agent (
proxy/Object.defineProperty
); - Modify object
configurable
,writable
Properties.
5, how inES5
In this caselet
和 const
?
1. The implementationlet
This can be done by self-executing functions.
2. Implementconst
This can be done with Object.defineProperty() to set writable.