First, some descriptions of JS object-oriented
1. The functions of object-oriented languages generally include: encapsulation, inheritance, polymorphism, aggregation (combining multiple objects to achieve more complex functions)
2. Objects generally consist of properties, methods, and constructors.
3. JavaScript does not have the concept of a class. To create an object, you only need to define a constructor for the object and create an object through it.
4. This pointer is also applicable to JS
Second, the declaration of the constructor
function Object_Name(arg1,arg2,...(argument list)){ //declare properties and initialize this.arg1 = arg1; this.arg2 = arg2; ...... //Methods for defining objects, generally there are getter, setter methods for calling and modifying properties, and other methods this.method1_name = function(null parameter or parameter list){ // method body } }
Third, the instantiation of the object
var object name = new Object_Name(parameter list);
Fourth, the object's properties and method calls
object_name.property_name or object.method_name
5. The prototype property
You can add object properties or methods outside the constructor through the prototype property
For example: (where Object_name is the constructor name, and the object name is the instantiated name)
1. Add new properties
Object_name.prototype. The new property name added (generally its initialization value is null);
2. Add new methods
Object_name.prototype.new method name = function(null parameter or parameter list) {
// method body
}