This article mainly gives a detailed analysis of 11 problems that are hard to understand in Javascript. If you need them, please refer to them and hope to help you.
1. Original and reference values
The original value is stored in the stack, and the reference value is stored in the heap. For example, the program:
The Code is as follows:
Function Person (id, name, age ){
This. id = id;
This. name = name;
This. age = age;
}
Var num = 10;
Var bol = true;
Var str = "abc ";
Var obj = new Object ();
Var arr = ['A', 'B', 'C'];
Var person = new Person (100, "stupid motto", 25 );
2. undefined and null
Undefined: the variable is Undefined; it is the exclusive value of the undefined type;
Null: The reference is not allocated; it is the exclusive value of the Null type.
Typeof (undefined) = undefined;
Typeof (null) = object;
Undefined = null;
Undefined! = Null;
Null instanceof Object = false;
Undefined instanceof Object = false;
Although there are Undefined and Null types, the following examples show that these two types are invisible, that is, we can only use their values:
Alert (undefined instanceof Undefined );
Alert (null instanceof Null );
3. pseudo Array
Features:
1) has the length attribute;
2) access data in the same index order as an array;
3) methods that do not have array-specific operation data, such as push, pop, slice...
Pseudo Arrays can all be converted to real arrays through Array. prototype. slice:
Var faceArray = {0: 'A', 1: 'B', length: 2} // standard pseudo array;
Var realArray = Array. prototype. slice. call (fakeArray );
Pseudo array in js: arguments, node. childNodes, document. getElementsByTagName ()...
In IE, node. childNodes cannot be converted using slice.
Pseudo array in Jquery: Jquery itself is a pseudo array:
Alert ($ ('. class1'). length); alert ($ ('. class1'). [0]. tagName );
4. Simple literal
Var a = 1; B = true, c = "ccc ";
The literal seems to have a type.
Alert (typeof a); // number
Alert (typeof B); // boolean
Alert (typeof c); // string
However, instanceof cannot be used.
Alert (a instanceof Number) // false
Alert (a instanceof Object) // false
Alert (B instanceof Boolean) // false
Alert (B instanceof Object) // false
Alert (c instanceof String) // false
Alert (c instanceof Object) // false
5. The prototype attribute of the function and the internal prototype attribute of the object instance
Each function (constructor) has a prototype attribute, and each object instance has an invisible attribute (mozilla makes it public, which can be obtained through _ proto) the internal prototype attribute pointing to the prototype attribute of the constructor. prototype can also have its own prototype attribute, which constitutes the prototype chain. objects are the top objects, so all prototype chains will eventually point to objects. prototype. when you access the properties/methods of an Object instance, you can start searching for the Object instance by yourself. If the Object cannot be searched, search for the Object along the prototype chain until the Object. prototype. prototype = null.
6. A secret of Constructor
The Code is as follows:
Var s = new function () {return "sss "};
Alert (s); // [object Object]
S = new function () {return new String ("sss ")};
Alert (s); // sss
Explanation of this Code:
As long as the constructor after the new expression returns (return) a referenced object (array, object, function, etc.), it will overwrite the anonymous object created by the new expression. If return is returned (return) an original type (undefined is actually the original type of return when no return is required). Then, an anonymous object created by new is returned.
7. Object creation process
The Code is as follows:
Function Person (name ){
This. name = name;
}
Person. prototype = {
GetName: function () {return this. name}
};
Var p = new Person ('hangsan ');
Decryption p creation process:
Pipeline creates a build-in object obj and initializes it;
Forward points the internal [[Prototype] of p to Person. prototype;
When p is used as this, the arguments parameter is used to Call the internal [[Call] Method of Person, that is, to execute the Person function body and return the return value. If no return is returned, the undefined function is returned;
If the Object type is returned in the previous step, this value is returned to p; otherwise, obj is returned.
8. self-owned and inherited attributes of an object
The Code is as follows:
Function Person (name ){
This. name = name;
}
Person. prototype = {
Type: 'Human ',
GetName: function () {return this. name}
};
Var p = new Person ('hangsan ');
Alert (p. hasOwnProperty ('type'); // false
P. type = 'ren ';
Alert (p. hasOwnProperty ('type'); // true
The running result is clear. The attributes of an object cannot modify the attributes of the same name in its prototype. Instead, an attribute of the same name is created and assigned a value to it.
9. Create a function object
Create a build-in object fn;
Set the internal [[Prototype] of fn to Function. prototype;
Set the internal [Call] attribute. It is an internal implementation method to process the function Call logic. (Point to the function body );
Set fn. length to funArgs. length. If the function does not have a parameter, set fn. length to 0;
Fn. prototype constructor points to fn itself;
Returns fn.
10. Principles of instanceof
Check whether a is an instance of B. It means that the prototype (prototype attribute of constructor) of B points to an object that is not in the prototype chain of.
11. Speculation about functions and objects
Alert (Function instanceof Function); // true
Alert (Function instanceof Object); // true
Alert (Object instanceof Function); // true
Alert (Object instanceof Object); // true
I have been thinking for a long time, but I haven't figured it out ......