Home > Article > Web Front-end > Introduction to JavaScript Design Patterns and Prototype Patterns (Object.create and prototype)_javascript skills
Prototype Mode Description
Description: Use prototype instances to copy and create new customizable objects; for new objects, you do not need to know the specific process of creating the original object;
Process: Prototype => new ProtoExam => clone to new Object;
Use relevant code:
Prototype.prototype.userInfo = function() {
Return 'Personal information, name: ' this.name ', age: ' this.age ', gender: ' this.sex '
';
}
Two or more personal information contents are now required:
Output return:
Prototype mode is generally used when the abstract structure is complex, but the content composition is similar, the abstract content can be customized, and the newly created object only needs to be slightly modified to meet the requirements;
Object.create Instructions
1>. Definition: Create an object that can specify a prototype object and can contain optional custom properties;
2> Object.create(proto [, properties]); Optional, used to configure the properties of the new object;
Can also contain set, get accessor methods;
Among them, [set, get] and value and writable cannot appear at the same time;
1. Create prototype object class:
How to use
1. Create an object with ProtoClass.prototype;
2. Use instantiated ProtoClass as prototype:
var obj2 = Object.create(proto, {
foo:{value:'obj2'}
});
obj2.aMethod(); //ProtoClass
obj2.foo; //obj2
3. Subclass inheritance:
SubClass.prototype.subMethod = function() {
Return this.a || this.foo;
}
This method can be inherited to the aMethod method of ProtoClass and executed;
To allow SubClass to read the member attributes of ProtoClass, SubClass needs to be changed:
//Other codes;
This method can obtain the member attributes and prototype methods of ProtoClass;:
Another method is to use an instantiated ProtoClass object as the prototype of SubClass;
function SubClass() {
}
SubClass.prototype = Object.create(proto, {
foo:{value: 'subclass'}
});
In this way, after SubClass is instantiated, you can obtain all the properties and prototype methods of ProtoClass, and create a read-only data attribute foo;
4. Another creation inheritance method has the same effect as Object.create using instantiated ProtoClass as prototype:
SubClass.prototype = new ProtoClass();
Object.create related instructions
Object.create is used to create a new object. When it is Object, the prototype is null, and the function is the same as new Object(); or {};
When it is a function, it has the same effect as new FunctionName;
//----------------------------------------
function func() {
This.a = 'func';
}
func.prototype.method = function() {
Return this.a;
}
var newfunc = new func();
//Equivalent to [same effect]
var newfunc2 = Object.create(Object.prototype/*Function.prototype||function(){}*/, {
a: {value:'func', writable:true},
Method: {value: function() {return this.a;} }
});
But newfunc and newfunc2 have different function references in the objects that create them.
newfunc is function func() {...}, newfunc2 is function Function {Native}
If proto is non-null, it is an instantiated value, that is, a value that has been new; most objects in JavaScript have a constructor attribute, which indicates which function this object is instantiated through;
propertiesField is optional and sets the member properties or methods that the newly created object may need;