1. Object.create
1>. Definition: Create an object that can specify a prototype object and which may contain optional custom attributes;
2> object.create (proto [, properties]); Optional, to configure the properties of the new object;
1. Proto: To create a prototype of a new object, it must be, nullable; This proto should have been created [new over], or object. prototype has value; 2. Properties: Optional, Structure: {propfield: {value: ' val ' |{}|function() {}, writable:true|false, Enumerable:true|false, Configurable:true|false,Get:function(){return10},Set:function(value) {}}} A custom property has the following four native properties: Value: Custom attribute value, writable: Whether the item value is editable, default is False, Obj.prodfield can be assigned if true, otherwise read-only; enumerable: enumerable; Confirurable: Configurable; can also containSet,Getaccessor methods; where, [Set,Get] with value and writable cannot occur at the same time;
1. Create a Prototype object class:
function Protoclass () { this. A = ' protoclass '; this. c = {}; This function () { }}
To create a prototype method:
function () { //this.a; // this.b ();
return THIS.A;}
How to use
1. Create an object with Protoclass.prototype;
var obj1 = object.create (protoclass.prototype, { true}})
Obj1 has the method of Protoclass prototype method Amethod;
Obj1.amethod (); // the output undefined method is accessible and the Protoclass member cannot access
But this method does not perform the member properties of a, B, C under Protoclass:
2. Prototype with an instantiated Protoclass:
var New Protoclass (); var obj2 = object.create (proto, { foo:{value:' Obj2 '}});
This creates a obj2 that has all the member properties of Protoclass A, B, C, and Amethod prototype methods; And added a Foo read-only data attribute;
// Protoclass //[object] // //Protoclass
Obj2
3. Subclass Inheritance:
function Subclass () { = object.create (protoclass.prototype, { ' subclass 'function () { returnThis this. Foo;}
This method can be inherited to the Protoclass Amethod method, executed;
var New subclass (); Func.amethod (); // Undefined, Protoclass member attribute not read, a,b,cFunc.submethod (); // Subclass
To allow subclass to read the member properties of Protoclass, subclass to change:
function Subclass () { Protoclass.call (this);} // other code;
This method can obtain the member property of Protoclass and the prototype method;
var New subclass (); Func.amethod (); // protoclassFunc.submethod (); // Protoclass
Another way is to use the instantiated Protoclass object as the prototype of subclass;
var New Protoclass (); function = object.create (proto, { ' subclass '}});
With this subclass instantiation, you can get all the properties and prototype methods of Protoclass and create a read-only Data property, Foo;
var New //Subclass//protoclass////[Object] // Protoclass
4. Another way to create an inheritance is to prototype the object.create using the instantiated Protoclass:
function Subclass () {
This //But this side can read and write
New Protoclass ();
Object.create related Instructions
Object.create is used to create a new object when prototype is null for object, acting with new object (); or {} is consistent;
When acting as function, the function is the same as the new functionname;
//1 Objectvaro = {}//equivalent tovarO2 =object.create ({});//The two constructor the same;//-----------------------------------------functionfunc () { This. A = ' func ';} Func.prototype.method=function() { return This. A;}varNewfunc =Newfunc ();//equal to [effect]varNEWFUNC2 =object.create (object.prototype/*function.prototype| | function () {}*/, {A: {value:' Func ', writable:true}, Method: {value:function() {return This. A;} }});
However, Newfunc and NEWFUNC2 are not the same as the function references of the objects in which they are created.
Newfunc for function func () {...},newfunc2 for function function {Native}
Object.create (proto[, Propertiesfield]):
Proto description, this value is required, can be null, if not set, will throw an exception;
Proto is non-null, which is the value that has been instantiated, that is, the value that has been new, and that most objects in JavaScript have a constructor property that describes which function the object is instantiated from;
Propertiesfield is optional and sets the member properties or methods that may be required for the newly created object;
[Design mode] JavaScript prototype mode: Object.create and prototype