1. History
Machine oriented
Process oriented: Break down the execution of a program into several steps
Object-oriented: Decompose the execution of a program into several things
2. Two basic concepts of object-oriented
Class: Representing a certain kind of thing, is abstract
Object: Represents a thing, is a specific
3. Quick Start
To express a person's complete information
1 <script> 2 3 //Process 4 var name = ' Zhangsan '; 5 var age =; 6 var marry = true; 7 alert (n Ame + age + marry); 8 9 //object-oriented ten function person{11 }12 var p1 = new Person (); p1.name = ' Lisi '; P1.age = 20;15 p1.marry = true;16 alert (p1.name + p1.age + p1.marry); </script>
In the process-oriented example, we used three variables to define a person's complete information, but these three variables are not necessarily related to each other, such as: I now find the variable age, although the data can be taken, but it is not clear that this is "who" ages.
In an object-oriented example, function person{} is a constructor. We can understand that each defined function person{} is a constructor for the person class.
Common system classes in 4.JS
1) String class
Length: Gets the string length
IndexOf (String): Gets the position where the argument appears in the string
SUBSTR (num1,[num2]): Intercept string
toLowerCase (): Convert to lowercase
toUpperCase (): Convert to uppercase
Replace (STR1,STR2): string substitution
2) Date class
GetYear (): Returns the year (sometimes with an error, the second is recommended)
getFullYear (): Return year
GetMonth (): Return month (0---11)
GetDate (): Returns the day ordinal of a month
GetDay (): Return days (0-6)
GetHours (): Returns the number of hours
Getminutes (): Returns the number of minutes
Getseconds (): Returns the number of seconds
Getmilliseconds (): Returns the number of milliseconds
3) Math class
Ceil (value): Returns the smallest integer greater than or equal to the number (for example, paging Math.ceil (4.8) =5)
Floor (value): Returns the largest integer less than or equal to the number
Min (value 1, value 2): Returns the minimum number
Max (numeric 1, value 2): Returns the maximum number
Pow (value 1, value 2): Returns the value of value 1 2 times Square
Random (): returns 0----------1
Round (value): Rounding
sqrt (value): Open square root
The methods under the math class are static methods
5. Custom class--Definition of class
Grammar:
function class name () {
}
In JS, there is no class definition statement, only function, each function, we can think of it as the constructor of the class of the same name
For example:
function person () {
}
It is the constructor of the person class
This function is also called the constructor
6. Custom classes--declaring objects
Syntax: var object =new class name ();
Keyword NEW: Instantiating an object, opening up the appropriate memory space
1 function person () {2 alert (' Hello '), 3}4 var p1 = new Person ();
When we execute the above example, we find that the person constructor is executed directly at the time of instantiation
7. Custom classes--about the use of object properties
Grammar:
Object. Properties;
Object [' attribute '];
1 function person () {2 alert (' Hello '), 3}4 var p1 = new Person (); 5 p1.name= ' Zhangsan '; 6 p1.age=30;7 alert (p1.name+p1.a GE);
In JS, object properties are dynamically added, and object properties can use "." or ['] these two forms of expression
The properties of an object can be any data type, such as: string, Number, object
8. Three keywords
Constructor: The constructor of the object is returned
typeof: Return Data type
Instanceof: Determines whether an object is an instance of a class
1 function person () {2 alert (' Hello '), 3} 4 var p1 = new Person (), 5 p1.name= ' Zhangsan '; 6 p1.age=30; 7 Alert (P1.name +p1.age); 8 9//Returns the constructor of the P1 object (p1.constructor), 11//Gets the data type of the P1 variable (typeof p1), 13//Determines if P1 is an instance of the person class, alert (P1 instanceof person);
9. The object's representation in memory
1//Example of function person () {3}4 var p1 = new Person (), 5 p1.name= ' Zhangsan '; 6 p1.age=30;7 8 var p2 = new Person (); 9 Alert (P2 . name);
In Example 1, when a P1 object is created, the corresponding heap space is opened for P1, and the name and age properties and values are added to the heap space pointed to by P1, and when the P2 object is created, the corresponding space is also opened for the P2 object, but the heap space pointed to by P2 is empty. So the P2 object does not have the name and age properties.
1//Example 2 2 function person () {3} 4 var p1 = new Person (); 5 P1.name = ' Zhangsan '; 6 p1.age = 30; 7 8 var p2 = p1; 9 alert (p2.name); p2.name = ' Lisi '; alert (p2.name); alert (p1.name);
In Example 2, the address of the heap saved in the P1 stack is assigned to P2, so that P2 also points to the address of the heap memory that P1 points to. Two objects point to the same heap of memory, so changing the properties of an object affects another object.
1//Example 3 2 function person () {3} 4 var p1 = new Person (); 5 P1.name = ' Zhangsan '; 6 p1.age = 30; 7 8 var p2 = p1; 9 alert (p2.name), p2 = null;11 alert (p1.name), alert (p2.name);
In Example 3, p2=null; Represents freeing the stack space occupied by P2, but the P1 space remains, P1 still points to the appropriate heap space, so deleting P2 does not affect P1 objects.
JS Object-Oriented 2