Gof authoritative explanation is that the prototype pattern is a pattern that creates multiple objects based on this object by cloning an object.
To implement this prototype pattern, you can use the method Object.create in ECMAScript 5 directly. It is not tight to create objects that extend out to specific objects, and you can add new properties.
var vehicle = { function () { this. Model); }}; var car = object.create (vehicle, { "model": { "Ford", true }}); Car.getmodel ()
Object.create The first parameter is the object to be expanded, and the two parameters are new properties. The detailed use method can be used to check MDN.
There is a problem traversing an enumerable property, and he iterates through all the elements in the prototype chain and needs to be filtered with object.hasownproperty.
Check the code below:
// only the property values in car are output. for (var in car) { if(Car.hasownproperty (name)) { Console.log (Car[name]) }}
When Object.create is not supported, such as IE8, we press the above example to simulate the prototype mode
varVehicle ={getmodel:function() {Console.log ("The model of this vehicle is:" + This. Model); }};varCar = (function(){ functionF () { This. Model = "Ford"; } F.prototype=vehicle; varf =NewF (); returnF;}) () Car.getmodel (); //Ford
JavaScript mode--prototype mode