JS Advanced
One, JS type of data
1. Basic types
JS has a total of 5 basic types, namely:
1) Undefined. He has only one value: undefined. If a variable is defined but does not assign a value to it, then the system defaults to assigning the variable to undefined.
2) Null. It also has only one value: null. He is a reference type, when a variable is ready to save the object for a variety of reasons not to point to an object, you can assign a value to the variable null, usually also do so.
3) number. A numeric type, which is a collection of integers and floating-point types in C #, what type it is, and how it is assigned, and what type of assignment he has in general. Note also that var num = 1.0; this time num is an integral type, and only when the decimal point has an exact value of not 0 is the floating-point type. There are other points of knowledge about the number type, such as: Nan indicates that the value is not a digit, and isNaN () can tell if the value passed in is of type A, parseint () can convert the incoming argument to a numeric type, if it contains a non-numeric type string, The string is automatically removed, the return value of parseint ("123blue") is 123, and the blue part of the string is ignored, so the parseint () method can be understood to attempt to convert the incoming argument to an integral type, and the converted part will be ignored. The Parsefloat () method is similar to the parseint () method.
4) Boolean. Boolean types, similar to the bool type in C #, have true and false two values, but there is no corresponding to a number like the 0 corresponding false,1 in C # corresponding to True.
5) String. A string type that holds a sequence of characters consisting of 0 to 16 bits of Unicode code.
2. Complex types
1) Object type. JS is the top-level "parent class" (because JS does not have the concept of a class, said he is the parent class is only to understand the convenience), is a set of data and methods (functions), but it does not have the traditional object-oriented language support classes and interfaces. The object type is essentially a unordered list of key-value pairs, similar to the collection, JSON format. It contains 7 methods, respectively:
Constructor () constructor?
hasOwnProperty (PropertyName) checks whether the property is in the current object
isPrototypeOf (object) to check if the object is a prototype
propertyIsEnumerable (PropertyName) checks whether the property can be cycled with for-in
ToString ()
ValueOf ()
There are several ways to create an object:
Through the New keyword: var s = new Object (); S.name= "James"; s.age=27;
Through the simple definition of JS: var s = {}; S.name= "James"; s.age=27;
Object literal notation: var s = {"name": "James", "Age": "27"}; The key of the Ps:json format data can be enclosed in double quotes, and value may not be enclosed in double quotes if it is not a string, but the recommended key and value are enclosed in double quotes to avoid unnecessary hassle.
Methods for accessing properties of Object objects:
S.name straight out.
s["name"; the advantage of using square brackets (similar to indexers) is that you can dynamically access properties through variables: var proname= "name"; Alert (S[proname]);
2) array type. is an ordered list of data
Different from the other arrays:
An array element can be of any type, and the element type of the same array can be different, equivalent to the list<object> in C #
Length can be arbitrarily changed
The length property of the array is read/write (you can use this to remove the array element)
Stack methods for arrays LIFO
Push () to Riga
Pop () Get out of the stack, and the number of elements in the array will change.
Array Queue method Advanced first Out
Shift () from the tail of the queue to take out
Unshift () from the tail of the queue to Riga
Sort
Sort () to arrange the data in the array in a certain order, and parameters can pass an anonymous method (similar to an interface)
Reverse () Flip arrangement
Connecting arrays
Concat () Example: Var colors=["A", "B"];var newcolor=colors.concat ("Yellow", ["C", "D"]); The result colors has 5 elements. If an array is passed in the Concate method, the array is split and the elements are added to the target array. If you pass in the JSON format data, a JSON data is considered an element to add an array.
3 function type. function is an object, the function name is a pointer
Declaration mode (3 kinds):
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 (parse two times)
The function type does not have overloads. A function type is essentially a data type, and as with other types, the latter assignment overrides (overrides) the previous assignment when the value is assigned multiple times. Multiple functions with the same name actually assign values to the same function object, and the latter assignment overrides the previous assignment, so the last function definition is executed.
You need to understand the execution environment and scope of the JS code and some other points of knowledge before you speak about function properties:
A The execution environment: the parent environment in which the current function (method) is located. Like a function executed under window. His execution environment is window. The real global execution environment is global, except that most browsers do not expose code access, but are indirectly accessed through windows.
b If such statements do not use a block scope, the scope of JS and C #, If,for, and so the code enclosed in curly braces can not form a block scope.
c when declaring a variable with VAR, he adds it to the nearest available environment and adds it to the parent environment without using VAR, which is why a variable without the VAR definition is a global variable.
d The statement will be executed first, no matter where you put it. Although the JS code is executed from top to bottom, when the statement is encountered, the compiler executes the declaration statement to ensure that other statements are executed without error due to a variable that is not declared.
e) garbage collection. Setting a variable of a saved object to null is equivalent to cutting off the relationship between the variable (stack) and the reference value (heap), which is automatically reclaimed by the garbage collection station.
Internal properties of function:
Arguments
He is an array that holds the parameters passed in.
Callee is a pointer that holds the function object that owns the arguments object, which is the heap address of the function, and when the function needs to call itself, it can use callee without appearing its own function name, thus reducing the coupling.
This
Point to the execution environment where the current function is located, that is, the scope at which the function is executed
Properties and methods of function objects
Length
Number of named arguments defined by the function
The name of the function. length
Prototype (prototype)
The real way to save all their instance methods
Apply ([scope to change])
Changing the scope of the function object is to change the value of this
Sample code
Copy Code code as follows:
function sum (x, y) {
alert (this);
return x + y;
}
Window.sum (1,2);
function CallS () {
CALLS.CALLSUM1 (1, 2);
}
CALLS.CALLSUM1 = function (x, y) {
alert (this);
var s = sum.apply (this, arguments);
SUM (1, 2);
return s;
}
CallS ();
The call () method is similar to the Apply () method
The above two methods are not inherited, you can expand the scope of the function, the most benefit is that the object and the method does not need to have any coupling relationship. The first argument passed in both is the scope to be changed, and the second argument is passed an array of arguments, and call passes in each named argument.
Two, value types and reference types
With so many basic types (except null for value types) and complex types (basically reference types), we need to understand why language designers design value types and reference types. What is the difference between the two?
1. The value type content length is fixed, the existence scope reference type of the saved value is not fixed, can store indefinite length data;
2. Value types can only be stored in a simple value, such as Integer, String, and so on. A reference type can store the heap address of an object and can cause multiple variables to point to the same object;
3. Also the most important point, the reference type can mitigate the stack's storage pressure (value types are stored on the stack).
4. In the JS syntax, basic data types cannot dynamically add properties and reference data types can dynamically add properties.
Three, the detection type key word
1.typeof
To determine the type of the base type returns TRUE or False
2.instanceof
To determine the type of a complex (reference) type returns TRUE or False
If you use it to judge the base type, you return False forever.
Iv. eval () method
The eval () method is quite powerful, and he is equivalent to a parser. It takes only one parameter, which is the JS code string to execute. When the parser discovers Eval (), he parses the parameters in eval () and inserts them into the location of the eval execution, which is equivalent to writing the JS code directly in the appropriate location.
V. Creating objects
1) Simple factory model
2 constructor mode. Each instance includes all the methods, which wastes memory.
3) prototype mode. Keep the method in the prototype so that all instances can invoke this method without having to save the method for each instance