JS Small dot

Source: Internet
Author: User


JS Dot 1:
function A (x) {
return x * 2;
}
var A;
alert (a);

JS is always the first to parse the declarative function, and then parse the variable, but not assign a value

Execution order:
1, analytic function A
2, declare variable var A; At this point A is not assigned, so a=function a


JS Dot 2:
The concept of no block in JS-for example, I in the for (var i-in array) is still a global variable

JS Dot 3:
function b (x, Y, a) {
ARGUMENTS[2] = 10;
alert (a);
}
B (1, 2, 3);

Analysis: An object can be referenced inside a function, which is a arguments-like array, but not an array. It represents the collection of parameters that the function actually receives. The corresponding parameters can be accessed by subscript. If you modify some properties of this object, such as Arguments[index], the value of the variable that is passed in index (subscript starting at 0) is also modified.

Arguments[2] Like an array, but not an array, the function is to assign a variable to the relative angular mark

JS Dot 4:
function A () {
alert (this);
}
A.call (NULL);

The call method accepts multiple parameters, and its role is to borrow others ' methods as its own method. This ensures that this can point to itself when executed. The second parameter of the call method to the last argument is passed to the borrowed function. The first argument is the borrowed object, and if the object is empty, it will be called as a global window object. That is, the this in the function points to the window.


JS Dot 5:
When a function with the same name is created with a definition, the function created later overrides the function created first. This difference is due to the working mechanism of the JavaScript interpretation engine.
Before executing any function calls, the JavaScript interpretation engine first registers the function created in the global scope with the definition, and then executes the function call in turn. Since the function is registered, the function defined later overrides the function defined first, so no matter where the calling statement is located, the function that is defined after is executed.
Conversely, for a declarative-created function, the JavaScript interpretation engine evaluates the variable as if it were to execute the code that invokes the variable, just as it would for any declared variable.
Since the JavaScript code executes from top to bottom, the code of the example function is defined first when the first example () call is executed, and when the second example () call is executed, the code of the example function becomes the code that is later defined.

JS Dot 6:

JS is an interpreted language rather than a compiled language, and the code is dynamically compiled and executed by the parser one line at a time instead of being compiled before execution
JS Edge compilation Side Execution

The declaration of a variable is processed during the precompiled period and is visible to all code during the execution period

The initialization process of a variable takes place during the execution period rather than the pre-compilation period, and execution time JS interpreter is interpreted in code order.

Declare global variables and functions before the JS code and assign values to the global variables, and within the function you should declare the variables and then reference them.


JS Dot 7:
All variables declared in a function, regardless of where they are declared, are defined throughout the function.


JS Dot 8:

To create an object:
The process of creating an object with Var anobject=new afunction () is actually divided into 3 steps:
1, create a new object
2, set the object's built-in prototype object to the prototype object referenced by the constructor prototype
3, the object is called as the This parameter constructor, the completion of the initialization of member settings and other tasks

After an object is established, any access and operation on the object is related to the object itself and the string of objects on its prototype chain, and is not dependent on the constructor function
In other words, the constructor is only for the two roles in which the dress object is to introduce a prototype object and initialize the object

Prototype chain:
Each object (the object enclosed in curly braces, excluding Function,array) Initializes a property within it, which is
_proto_, when we access a property of an object stone, if this object does not exist inside this property, then will go to proto inside to find this attribute,
This proto will have his own proto, just keep looking.


JS Dot 9: Data type detection

To determine what data types might think:
Constructor, typeof, Instanceof, Object.prototype.toString.call ()

1. Through the constructor property
The method of obtaining a type through the constructor property is easily modified and should not be used to determine the type.

2, through the TypeOf
typeof is an operator, not a function.
The practical application of typeof is to detect whether an object has been defined or has been assigned a value.
such as if (typeof a!= "undefined") {}, do not use if (a) because if a does not exist (not declared) then an error occurs.
typeof generally only returns the following results when detecting object types:
number,boolean,string,function,object,undefined.
For Array,null, custom objects, and so on, use TypeOf to return object,
This is precisely the limitation of TypeOf.

3, through the instanceof
Use the instanceof operator to determine whether an object is an instance of a class.
If obj instanceof class returns True, then the prototype of class is the same object as a prototype on the OBJ prototype chain.
That is, obj is either created by class or by a subclass of class.

4, through the Object.prototype.toString.call ()

The Object.prototype.toString.call () function is:
1. Gets the class name (object type) of the object.
2. Then combine [object, get class name] and return.
Can be used to determine array,date,function and other types of objects


JS Dot 10:

Closures:
function A () {
var i=0;
Function B () {
alert (++i);
}
return b;
}
var C = A ();
C ();

Code Features:
1, function b is nested inside function a
2, function a returns function B

When an intrinsic function of a function is referenced by a variable outside of the function, a closure is created

The function of a closure is that after the function is finished and returned, the garbage collection mechanism of JavaScript does not reclaim the resources that the current function consumes
Since the execution of the function's intrinsic function requires a dependency on the variables in the function.

JS Small dot

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.