For more information about JS advanced notes, see. JS advanced
I. JS Data Types
1. Basic Type
JS has five basic types:
1) Undefined. It has only one value: undefined. If a variable is defined but not assigned a value, the system will assign the variable undefined by default.
2) Null. It also has only one value: null. It is a reference type. When a variable preparing to save an object has not pointed to an object for various reasons, you can assign a null value to this variable. This is usually the case.
3) Number. The value type. It is a set of integer and floating point types in C #. The specific type depends on the Value assignment. In addition, note that var num = 1.0; at this time, num is an integer, and only floating point type is used when there is a definite value not 0 after the decimal point. There are other knowledge points about the Number type. For example, NaN indicates that the value is not a Number, and isNaN () can determine that the input value is of the Number type. parseInt () you can convert the input parameter to the numeric type. If the input parameter contains a non-numeric string, the string is automatically removed. The return value of parseInt ("123 blue") is 123, the blue part of the string is ignored. Therefore, the parseInt () method can be understood as an attempt to convert the input parameter to an integer. If it cannot be converted, it will be ignored. The parseFloat () method is similar to the parseInt () method.
4) Boolean. Boolean Type, similar to the bool type in C #, which has two values: true and false. However, there is no correspondence between values such as 0 in C # And False in 1 and numbers.
5) String. String type. It stores a string consisting of 0-16 UNICODE codes.
2. complex types
1) Object type. Is the top-level "parent class" of JS (because JS does not have the concept of class, it is said that it is a parent class just for understanding convenience), is a set of data and methods (functions) but it does not have the classes and interfaces supported by traditional object-oriented languages. The Object type is essentially an unordered list of key-value pairs, similar to a set in json format. It contains seven methods:
Constructor () constructor?
HasOwnProperty (propertyName) check whether the property is in the current object
IsPrototypeOf (object) checks whether the object is prototype.
PropertyIsEnumerable (propertyName) checks whether attributes can be cyclically used for-in.
ToString ()
ValueOf ()
You can create an Object in multiple ways:
Use the new Keyword: var s = new Object (); s. name = "james"; s. age = 27;
Simple JS definition: var s ={}; s. name = "james"; s. age = 27;
Object literal representation: var s = {"name": "james", "age": "27"}; PS: Keys of json data can be enclosed in double quotation marks, if the value is not a string, you do not need to enclose it in double quotation marks. However, we recommend that you enclose both the key and value in double quotation marks to avoid unnecessary troubles.
To access the attributes of an Object, follow these steps:
S. name.
S ["name"]; the advantage of square brackets (similar to the indexer) Is that attributes can be dynamically accessed through variables: var proName = "name "; alert (s [proName]);
2) Array type. Is an ordered list of data
Different from other Arrays:
Array elements can be of any type, and the element types of the same array can be different, which is equivalent to List in C #.
The length can be changed at will.
The length attribute of the array can be read and written (this can be used to delete array elements)
The stack method of the array is followed by first-in-first-out
Push () to add
Pop () is taken out of the stack header, and the number of elements in the array changes.
Array queue method first-in-first-out
Shift () from the end of the queue
Unshift () from the end of the queue to add
Sort
Sort () is in the forward order, and the data in the array is arranged in a certain order. parameters can pass an anonymous method (similar to interfaces)
Reverse () reverse sorting
Connection Array
Concat () Example: var colors = ["a", "B"]; var newcolor = colors. concat ("yellow", ["c", "d"]); colors has five elements. If an array is input in the concate method, the array is split and the elements are added to the target array. If data in json format is input, a json data is regarded as an element and added to an array.
3) Function type. The function is an object and the function name is a pointer.
Declaration method (3 types ):
Function sum (x, y) {return x + y ;}
Var sum = function (x, y) {return x + y;} // function expression
Var sum = new Function ("x", "y", "return x + y;"); // object creation, not recommended (resolved twice)
Function type is not overloaded. The Function type is essentially a data type. Like other types, when multiple values are assigned, the next value will overwrite (replace) the previous value. Multiple Functions with the same name are actually assigning values to the same function object. The latter assignment will overwrite the previous assignment, so the execution is the last function definition.
Before talking about the Function Attributes, you must first understand the execution environment and scope of JS Code and other knowledge points:
A) execution environment: the parent environment where the current function (method) is located. For example, the execution environment of the function executed in window is window. The real Global execution environment is Global, but most browsers do not disclose code access, but access is indirectly through windows.
B) if and other statements do not use block scopes. JS scopes are different from C #. if and for codes enclosed in braces cannot form a block scope.
C) when a variable is declared using var, it is added to the nearest available environment. If var is not used, it is added to the parent environment, this explains why a global variable does not need to be defined by var.
D) The declaration statement will be executed first, no matter where you put it. Although JavaScript code is executed sequentially from top to bottom, the compiler will first execute the declaration statement in case of declared statements, to ensure that no error is reported because no declared variable is encountered during execution of other statements.
E) garbage collection. Setting the variable of a stored object to null is equivalent to breaking the relationship between the variable (stack) and the reference value (HEAP). The garbage collection bin will automatically recycle it.
Function internal attributes:
Arguments
It is an array that stores input parameters.
Callee is a pointer that stores the function object that owns this arguments object, that is, the heap address of this function. When the function needs to call itself, you can use callee, you do not need to have your own function name to reduce coupling.
This
Point to the execution environment of the current function, that is, the scope of the function during execution
Attributes and methods of function objects
Length
Number of name parameters defined by the function
Function name. length
Prototype)
Save the authenticity of all their instance methods
Apply ([scope to be changed])
Change the scope of the function object, that is, change the value of this.
Sample Code
The Code is as follows:
Function sum (x, y ){
Alert (this );
Return x + y;
}
// Window. sum (1, 2 );
Function cballs (){
CILS. callSum1 (1, 2 );
}
CILS. callSum1 = function (x, y ){
Alert (this );
Var s = sum. apply (this, arguments );
// Sum (1, 2 );
Return s;
}
CILS ();
The call () method is similar to the apply () method.
The above two methods are not inherited and can expand the scope on which the function depends. The biggest advantage of doing so is that the object and method do not need to have any coupling relationship. The first parameter to be passed in is the scope to be changed. The second parameter apply is a parameter array, and the call is passed in to each named parameter.
Ii. Value Type and reference type
After talking about so many basic types (except null, all are value types) and complex types (basically all are reference types), we need to understand why Language designers need to design value types and reference types. What is the difference between the two?
1. The content length of the value type is fixed. The content length of the stored value range reference type is not fixed. data with an indefinite length can be stored;
2. The value type can only store values, such as integer and string. The reference type can store the heap address of an object and point multiple variables to the same object;
3. It is also the most important point. The reference type can relieve the Storage pressure on the stack (the value type is stored in the stack ).
4. In JS syntax, attributes cannot be dynamically added for the basic data type, but attributes can be dynamically added for the referenced data type.
3. Keyword of detection type
1. typeof
Returns true or false to determine the type of the basic type.
2. instanceof
Returns true or false to determine the type of a complex (reference) type.
If it is used to determine the basic type, false is always returned.
Iv. eval () method
The eval () method is quite powerful, and it is equivalent to a parser. It only accepts one parameter, that is, the JS Code string to be executed. When the parser finds eval (), it will parse the parameters in eval () and insert them to the eval execution location. The effect is equivalent to Directly Writing JS Code at the corresponding location.
5. Create an object
1) simple factory Model
2) constructor mode. Every instance includes all the methods, which wastes memory.
3) prototype mode. Save the method in the prototype so that all instances can call this method, instead of saving this method for every instance.