JS object (hand notes)

JS object (hand notes)

1. What is an object?
An object is a specific thing, a set of disorderly related attributes and methods. For example, a person is an object. His attributes can include gender and age. His method can be to play games and cook. . Attributes are the characteristics and methods of an object, its behavior, what it will do, and what skills it has. So the object is composed of two parts, one is the attribute and the other is the method. Everything is an object, as long as it is concrete, it can be abstracted as an object.
For example, the hero of Yasuo in the League of Legends, he can actually be abstracted as an object, because he also has characteristics and behaviors:
Insert picture description here
Insert picture description here
2. Why do we need to introduce objects:
We usually store values ​​by declaring a variable or array, etc. It’s okay, but when we need to store a person’s complete information, using these two data types to store variables is very inconvenient. It is troublesome to require multiple variables. Arrays can’t be fully and beautifully reflected. If you use objects, It's very clear.
Example: Insert picture description here
3. Three ways to create an object:
a. Using literals to create an object contains properties and methods:
Insert picture description here
b. Creating an object through new Object:
Insert picture description here

c. Create objects through the constructor: (function names are capitalized according to the specification, used to distinguish between ordinary functions and constructors, no return is required)

Insert picture description here
The execution flow of new in the execution process:
1. Create a new empty object in memory.
2. Let this point to this new object, execute the code in the constructor, add properties and methods to this object
4. Return this row object.
4. Traverse objects:
Insert picture description here
4 Built-in objects: Objects in
js can be divided into three types: custom objects, built-in objects, browser objects;
String object:
length Get the length of the string
charAt (index) Return the character at the specified position of the string, The subscript starts from 0
indexOf(searchValue) returns the position of the first occurrence of the substring in the string
lastIndexOf(searchValue) returns the position of the last occurrence of the substring in the string
substring(start[,end]) returns the specified starting position of the string To the ending position of the substring
substr(start[,length]) intercept the specified number of characters from the specified position
toLowerCase() Get the lowercase form of the string
toUpperCase() Get the uppercase form of the string
split ([separator[, limit]) according to The specified delimiter is divided, and an array is returned. Limit is used to limit the number of
Math objects:
member function
random() returns a random number between 0-1
max ([value1[,value2,...]]), whichever is the maximum value
min ([Value1[,value2,…]]) Take the minimum
abs(x) Get the absolute value of x
round(x) Get the rounded integer of
x ceil(x) Round up
floor(x) Round down
Date object:
getFullYear() Get the 4 digits of the year, such as 2020
getMonth () Get the month, range 0~11 (0 means January, 1 means February, and so on)
getDate() Get each day of the month, range 1~31
getDay() Get the week, range 0~6 (0 means Sunday, 1 means Monday, and so on)
getHours() Gets the number of hours, returns 0~23
getMinutes() Gets the number of minutes, range 0~59
getSeconds() Gets the number of seconds, range 0~59
getTime() Returns the current time Milliseconds, timestamp

5. The usage of proptotype:
You can use proptotype on the type to add behavior to the type. These behaviors can only be reflected on instances of types.
The allowed types are Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String. In the
Insert picture description hereabove code, the prototype object of the constructor Animal is the prototype object of the instance objects dogOne and dogTwo. Add a color attribute to the prototype object. As a result, the instance object can read the attribute, only if the instance object of Animal will share the color attribute.
The properties of the prototype object are not properties of the instance object itself. As long as the prototype object is modified, the change will immediately be reflected in all instance objects.
If the instance object itself has a certain property or method, it will not go to the prototype object to find this property or method. (Of course there are other usages, you can learn more by yourself)

Guess you like

Origin blog.csdn.net/m0_46188681/article/details/106147171