Three main features of object-oriented:
- Packaging
A) put some properties and methods into one object
2. Inheritance
A) The inheritance in JS refers to:
-
- An object has no methods and properties, and another object has
- Take the properties and methods of another object, take it yourself, and that's the inheritance.
b) Mixed-type inheritance for ... in
1. The properties and methods of the parent class are all
3. polymorphic
A) JS is not reflected in the corresponding
b) More common in strongly typed languages
c) The variable of the practical parent class accepts the object of the child class
D) The property method of the parent class is shared for all subclasses
How objects are created
1. Create an Object
Four methods:
1, the literal creation
Create one with one and cannot be reused. Can cause code Shanyu, waste of resources
2. Built-in constructors create objects
1. Built-in constructors create objects
1. var p = new Object ();
2.var arr = new Array ();
3. Created objects are empty objects, to manually add properties, resulting in code duplication
3, Package simple factory function (not recommended)
function creatobj (name, age) { var obj = { boj:name; Obj:age; Sayhello:function () { console.log ('); } } return obj;}
4. Custom constructors
function Porple (data1,data2) { this. xx = data; }
What is a constructor used for?
In JavaScript, constructors are used to add properties to an object and initialize properties.
The creation process of an object
var p = new Person();
Take the above P object creation as an example:
Create an object with the New keyword first, similar to the {}
one used, and the object created at this time is a "no member" object. Here are two points to note:
new
object created using the keyword, the type of the object is the name of the function that created the constructor used by this object
- Using the
{}
Create object, the type of the object must be the Object
equivalent of using thenew Object()
To initialize a member with a constructor
- At the beginning of the constructor call, there is an assignment operation, which is to let
this = 刚创建出来的对象
- In the constructor,
this
it represents the object that was just created
Add members to an object in the constructor, using the object's dynamic properties
Custom constructors
function Porple (DATA1,DATA2) {
this.xx = data;
}
- Concept: Used to instantiate an object and assign a value to the initialized object
- Constructor name first letter uppercase
- Constructors are typically used with the new keyword
- The constructor return value defaults to the newly created object, if the return value is set manually
A) The return value type returns the created object if it is not of type Object
b) Returns an object if it is of type Object
A) object literal {}, creating object
b) Custom constructors, creating objects
- Constructors are also functions, which are commonly used to initialize objects
- New to create an object
- Constructors are used to initialize functions
- To capitalize the constructor name,
c) The execution process of the constructor function
- Create an object using the New keyword
- Call the constructor to assign the newly created object to the This keyword of the constructor
- Within the constructor utility this adds a member to the newly created object
- The newly created object is returned by default
- If you return a null value, or return a basic type of data, the newly created object is still returned
A) return undefined and Null both return new objects
- If the return is of type object, it will return the objects after return
- Function name parentheses are called functions
- Do not write parentheses is to assign the whole code of the function to another variable
- If the function is called without a constructor argument, the constructor does not create a new object, and this will point to the window, and the added property will be added to the window
- If the constructor has no arguments, it can be called without writing parentheses
Object members: Properties and methods
Prototype
- Every function, when defined, will have an object associated with it being created.
- Each object created by the constructor is associated with the default and the mysterious object of the constructor function.
- When a method is used to access a property or method, the property and method are found within the current object
- If it is not found within the current object, go back to the mysterious object it is associated with and look inside
- Accessing the prototype of a constructor
A) constructor. prototype
2 . Constructor . prototype. New Property = Property value
3. The attributes that you and the prototype have, take precedence over your own
3. Create an object called an instantiated object
A) The process of creating object creation objects from a constructor function
4. Example
A) An instance of this constructor is an object that is instantiated by a constructor function
5. Practical methods of prototyping:
A) Add attributes to the prototype object using the object's dynamic characteristics
6. Replace the prototype object directly
Porple.prototype = {
Name: ' Jack ',
}
A) problems with direct substitution of prototype objects
-
- The prototype of the object created before the replacement prototype and the prototype of the object created after the replacement prototype are not the same
- Replace is not modified
b) When the point syntax is assigned to a property, the prototype is not searched.
c) when assigning a value using Point syntax , if the property does not exist in the object, it will be added to the object and will not be found in the prototype
d) If the attribute of the prototype total score is a property of a reference type, then all objects share the property, and an object modifies the reference
A member of the type, all objects will be modified
E) Properties are not typically placed in the prototype
7. Accessing prototypes through constructors
A) constructor. prototype
8. Accessing prototypes through objects
A) object. __proto__ // not recommended
9. Constructor: Properties of the prototype object
JS Advanced. 02 Object-oriented, object creation, constructors, custom constructors, prototypes