Prototype mode: Personally, I think it is no different from the combined inheritance in inheritance. It should be noted that:
Objects inherited under the prototype mode, the prototype functions of the protytype of the shared base class occupy the same memory space, so if you move one, you will move all.
var SuperClass = function ( arg1 , arg2 ) { this.val1 = arg1; this.arg2 = arg2; }; SuperClass.prototype = { fun1 : function(){ }, fun2 : function () { } }; var SubClass = function ( arg1 , arg2 ) { SuperClass.call( this, arg1 , arg2); }; SubClass.prototype = new SuperClass(); // Override inherited method SubClass.prototype.fun1 = function () { };
Prototypal inheritance is actually multiple inheritance. If you look closely, it feels similar to the previous builder pattern. It consists of multiple objects. The builder pattern is more flexible by instantiating objects from new.
// Here prototypal inheritance is a prototype chain with each object's properties as subclasses.
function prototypeExtend(){ var F = function () {}, args = arguments, i = 0, len = args.length; for( ; i<len ; i++ ){ for( var j in args[i] ){ F.prototype[j] = args[i][j]; } } return new F(); } var s1 = { speed : 20, swim : function () { console.log( 'Swimming speed' + this.speed ); } }, s2 = { run : function ( n ) { console.log( 'Running speed:' + n); } }, s3 = { jump : function () { console.log( 'jump'); } }; var t = prototypeExtend( s1, s2, s3); t.swim(); t.run( 10 ); t.jump();
// Multiple inheritance, copy the properties of the object to itself
// Multiple inheritance copies multiple object properties Object.prototype.mix = function () { var i = 0, len = arguments.length, arg; for( ; i<len ; i++ ){ arg = arguments[i]; for( var property in arg ){ this[property] = arg[property]; } } }; var book1 = { color: "bluc" }, book2 = { name: "java" }, book3 = { price: 100 }; book1.mix( book2 , book3 ); console.log( book1 );