JS (data type, pre-parsing, closure, scope, this)

Source: Internet
Author: User

1. Differences in data types
Basic data type: Number, String, Boolean, null, undefined
Reference data type:
1-1: Objects Data Type: Object, Array, Date, Regexp, String, Boolean, etc.
1-2: Functions Data type function
2. Pre-parsing (variable promotion)
In the current scope, JS from top to bottom execution, first declare and define the Var and function keywords
2-1: Declaration (DECLARE): var num = 12; In the pre-interpreted state for var num = undefined;
2-2: definition (defined): assignment
2-3:var: Define assignments only in advance of pre-parsing and during code execution
2-4:function: Pre-parsing completes declaration + definition (code inside the function---stored in the newly opened heap memory as "string")
2-5: Heap Memory: Reference data type space where new addresses are opened (values that store reference data types)
2-6: Stack Memory: Stores the value of the base data type, provides the JS code execution Environment \
3, scope ("Closure")
A new private scope is formed when the function executes (assigning parameters---Pre-parsing (variable elevation)---code execution)
The new private scope, the protection of the private variables inside the outside interference, the protection mechanism of this function---"Closure"
3-1: Private variables: Variables declared in private scope are private (scoped protected), formal parameters are private variables (performed before pre-parsing)
3-2: Variable lookup: A variable appears in the private scope
3-2-1: First Look for private variables (whether defined in private scope)
3-2-2: If it is not a private variable, look up to the parent scope
3-2-3: If the parent scope does not, then continue to look up, until the window
3-3: Find the ancestor scope: the position of the ancestor scope, and where the function is executed without relation, and where the heap memory of the current function object is defined
(in that definition, who is the ancestor scope of the current function)
4. Do not destroy scopes
The function executes, forms a private scope, returns a value of "reference data type" in the private scope, and has variable acceptance outside. The current scope cannot be destroyed
1, function execution, return a value of "reference data type", but there is no variable accepted outside, then the current scope is "Do not destroy immediately", when browser idle time automatically destroyed
2. In general, the function execution will be destroyed automatically (GC)
3, the scope does not destroy the second case (assigned to a DOM element, the event)
Odiv.onclick = (function () {
return function () {
odiv.innerhtml = 0;
}
})();
Preconditions, return values in a scope, return
5, this
This in the general value function
This is not related to WHO and "function" (defined there) and (where executed)
1, self-executing function this is always window
2, to the element binding method, when the event is triggered and executes the corresponding method, the method of this is the current element
3, no matter where the function executes, we only need to see if there are ".", if any, "." Before the function name. Who's in front of this?

Pre-parsed N cases:
1, regardless of whether the judgment conditions are established, will be pre-analysis
2, only the "=" to the left of the pre-parsing, the right is the value, do not parse
3. Self-executing function does not pre-parse
4. The return value (function) after return in the function body is not pre-parsed, but the code following it needs to be expected West
5. Pre-analytic detachment the same script works quickly
6. If the duplicate name is found when pre-parsing (no re-declaration, but need to be redefined)

Under what circumstances function functions are not pre-parsed
1, self-executing functions (function () {}) ();
2. Odiv.onclick = function () {} to the right of the equals sign
3. Return function () {}

Convert other data types to a Boolean type rule (0, NaN, "", Null, undefined to false) others are true

The difference between var num = 12 and num = 12 in the global scope
1, with Var in the code before the execution of pre-parsing, no var can not
2. num = 12 means to add a property name named Num to the window, the assignment attribute is---window.num = 12;
3, var num = 12; At the global scope, it is also equivalent to defining a property name called num for window, with a property value of 12


Little Practice
[] == []; ! [] == []; [] ==false; ! [] ==false; Console.log (Ainchwindow);    FN (); functionfn () {Console.log (1);} varfn;    FN (); functionfn () {Console.log (2);}    FN (); varfn = 13; functionfn () {Console.log (3);}    FN (); varnum = 2; varobj ={num:4, fn: (function () {             This. Num *= 2; Num*= 2; varnum = 3; return function () {                 This. Num *= 2; Num*= 3;            Console.log (num); }}) (), DB1:function () {             This. Num *= 2;    }    }; varfn =Obj.fn;    Console.log (num);    FN ();    Obj.fn ();    Console.log (num); Console.log (obj.num);

JS (data type, pre-parsing, closure, scope, this)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.