A comprehensive understanding of js variables in native javascript learning
- 2021-07-02 23:35:56
- OfStack
1. Naming variables
Nomenclature of methods (hump nomenclature)
All lowercase: All underscores between words (my_namespace)
Mixed case: The first word starts with lowercase and other words start with uppercase.
Rules
First character English letter or underscore
Form an English alphanumeric underline
Taboo JavaScript Keyword Reserved Word
2. Declaration of variables
Display declaration: var keyword
Bad habit: No type duplicate declaration Implicit declaration No direct assignment declaration
Positive solution: first declaration, then read and write, first assignment, then operation
3. Variable types
Value type
A) The occupied space is fixed in the stack
B) What holds and assigns a value is the value itself
C) Use typeof to detect the type of data
D) Primitive type data is a value type
Reference type
A) Occupied space is not fixed in the heap
B) Save and copy a pointer to an object
C) Use instanceof to detect the type of data
D) Objects constructed using the new () method are reference types
4. Variable scope
Global variables contain variables defined outside the function. Variables defined inside the function are not var. Not recommended (can be called anywhere)
Local variables contain the parameter variables of the variable function declared using var inside the function body (inside the current function body)
Scope chain: inner function can access local variables of outer function
Outer function cannot access local variables of inner function
Lifecycle: The global variable 1 exists unless it is displayed and deleted
Local variables are declared until the function runs or is displayed and deleted
Reclaim mechanism flag clear reference count