Things are divided into abstract things and concrete things : abstract things are general; concrete things are specific
Object-oriented thinking features: 1. Extract (abstract) the attributes and behaviors shared by objects and organize (encapsulate) them into a class (template); 2. Instantiate the class and obtain the object of the class
Object : In JavaScript, an object is an unordered collection of related properties and methods. All things are objects, such as strings, numbers, arrays, functions, etc.
Objects are made up of properties and methods :
Attributes: the characteristics of things, represented by attributes in objects (common nouns)
Method: the behavior of things, represented by methods in objects (common verbs)
Class class: used to declare a class, and then instantiate objects with this class
The class abstracts the public part of the object, which generally refers to a certain class (class)
The object refers to a specific one, by instantiating a specific object
create class
Grammar: class name{//class body} The first letter of name is capitalized
Create an instance: var xx=new name(); Note: the class must use new to instantiate the object
Class constructor() method : it is the constructor of the class (the default method), which is used to pass parameters and return the instantiated object. When the object instance is generated through the new command, this method is automatically called. If there is no display definition, the class will automatically give us Create a constructor()
class class name{
constructor (parameter 1, parameter 2...){
//function body
}
method name() {
//content
}
}
var name=new class name();
Note: Constructor() is filled with attributes, not methods
class inheritance
Syntax: class Father{}//parent class
class Son extends Father{}//The child class inherits the parent class
super keyword: used to access and call functions on the parent class of the object, you can call the constructor of the parent class, or call the ordinary function of the parent class
In inheritance, if instantiating a subclass outputs a method, first check whether there is such a method in the subclass, and if so, execute the subclass first. If not, go to the parent class to find it, if it exists in the parent class, use the method of the parent class (nearest principle)
When a subclass uses super in its constructor, it must be placed before this (the constructor of the parent class must be called first, and then the constructor of the subclass must be used)
Points to note: 1. In es6, the class does not have variable promotion, so the class must be defined before the object can be instantiated through the class; 2. The shared properties and methods in the class must be used with this
Constructor : Some members can be added to the constructor, either on the constructor itself or on this inside the constructor. Members added by these two methods are called static members and dynamic members respectively
Instance members are members added through this inside the constructor Instance members can only be accessed through the instantiated object, not through the constructor
Dynamic members: Objects created inside the constructor can only be accessed by instantiated objects
Static members: members added to the constructor itself can only be accessed by the constructor itself
The constructor is very useful, but there is a problem of wasting memory space, because the method in the object is a complex data type, which will create a new memory space to store
constructor prototype prototype
The function assigned by the constructor through the prototype is shared by all objects
js stipulates that each constructor has a prototype attribute that points to another object. Note that this prototype is an object, and all properties and methods of this object will be owned by the constructor
We can define those invariant methods directly on the prototype object, so that all object instances can share these methods
A prototype is an object whose purpose is to share methods
In general, public properties are defined in the constructor, and public methods are placed on the prototype object
Object prototype __proto__
The object will have a property __proto__ pointing to the prototype object of the constructor. The reason why our object can use the properties and methods of the prototype object of the constructor function is because the object has a __proto__ prototype.
(1) The constructor has a prototype object prototype;
(2) The constructor prototype object prototype has a constructor pointing to the constructor itself;
(3) The constructor can add methods through the prototype object;
(4) The instance object created by the constructor has a __proto__ prototype pointing to the prototype object of the constructor
The prototype object this points to: 1> In the constructor, this inside points to the object instance; 2> this inside the prototype object function points to the instance object
Prototype chain:
Extend the built-in object: You can use the prototype object to extend the custom method of the original built-in object, such as adding the function of customizing the sum of even numbers to the array
Note: Array and string built-in objects cannot override the operation Array.prototype={} for prototype objects, only Array.prototype.xxx=function(){}
inherit
(1) call(): Call this function and modify the point of this when the function is running
fun.call(thisArg,arg1,arg2……)
thisArg: the object pointed to by the current calling function this arg1, arg2: other parameters passed
(2) Borrow the constructor to inherit the parent type attribute
Core principle: point the this of the parent type to the this of the subtype through call(), so that the subtype can inherit the properties of the parent type
The essence of class: the essence of class is still function
ES5 new method
1> Array method:
Iteration (traversal) methods: forEach(), map(), filetr(), some(), every();
array.forEach(function(currentValue,index,array){})
currentValue: the value of the current item in the array
index: the index of the current item in the array
arr: the array object itself
array.filter(function(currentValue,index,array){})
The filter() method creates a new array, and the elements in the new array are checked by checking all elements in the specified array that meet the conditions, mainly used to filter the array
Note that it directly returns a new array
currentValue: the value of the current item in the array
index: the index of the current item in the array
arr: the array object itself
array.some(function(currentValue,index,array){})
Used to check whether the elements in the array have objects that meet the conditions
Note that it returns a boolean value, if the element is found, it returns true; if not, it returns false
If the first element that satisfies the condition is found, the loop is terminated and no more searches are made
currentValue: the value of the current item in the array
index: the index of the current item in the array
arr: the array object itself
object method
Object.defineProperty() defines new properties in an object or modifies existing properties
Object.defineProperty(obj,prop,descriptor)
obj: required, target object
prop: required, the name of the property to be defined or modified in the form of a string
descriptor: Required, the characteristics of the target attribute: written in the form of an object {}
value: Set the value of the attribute, the default is undefined
writable: Whether the value can be rewritten. true|false default is false
enumerable: Whether the target property can be enumerated. true|false default is false false does not allow traversal
configurable: Whether the target property can be removed or the property can be modified again. true|false The default is false false It is not allowed to modify the characteristics inside