1. Object: A structure describing the properties and functions of an object in the real world in a program
Object-oriented programming, that is, to define the properties of the corresponding objects, to achieve the corresponding functions. An object specifically represents an object in the real world.
* A structure that encapsulates the properties and functions of a thing.
For example, our Phone: properties:
Screen Size: 5.5
Processor: A8
Running Memory: 2G
Storage: 64G
Contacts: ["Xiao Zhang": 12312312345,
"Amy": 32132154321,
"Xiao Jia": 23423423456]
Function: Call (phone number) {}
Send SMS (phone number, msg) {}
2. How to create an object
(1), Direct volume creation
var obj={
Property Name 1: Property value 1,
Property Name 2: Property value 2,
Method Name 1=function () {...},
Method Name 2=function () {...}
}
Use: When creating only one individual object
Note: JS bottom, all objects are stored in a hash array!
(2), using the constructor to create multiple unified structure objects, only one time to define the structure, you can repeatedly create the same structure of the object
How to use constructors: 2 steps:
STEP1: Defining constructors
function type name (attribute parameter 1, property parameter 2,...) {
this. property name 1 = parameter 1;
this. Property Name 2 = parameter 2;
This. Method name =function () {...}
}
STEP2: Call constructor function to create Object!
var obj=new type name (attribute parameter 1, attribute parameter 2);
Note: This refers to the object being created
Memory behavior of the new constructor
1. New creates an empty object
2. Call the constructor to add properties and methods to the new object
3. Set the __proto__ property of the new object to the prototype object of the constructor
4. Return the address of the new object to the caller
(3), create an empty object first, and then set the property
var obj=new Object (); Create an empty object
Obj. attribute 1= value 1;
Obj. attribute 2= value 2;
Obj. Method =function () {...}
Use: When you create an object, you do not know the structure in the object temporarily
You can create an empty object first, and then gradually append properties and methods (typically not used).
Fixed routines: Traversing all properties and methods in an object: for in structure
3. Determine whether the specified attribute exists in the object
(1), "attribute name" in obj:
If obj is a primitive type variable, you can new empty wrapper type object, and then detect
(2), if (obj. Method name ===undefined): property name does not exist
Specifically verify that the method name exists-most frequently used!
can be abbreviated as if (obj. Method name)
OOP Object-oriented programming