function Child(){ Parent.call(this) } Child.prototype=new Parent(); //优化 function Child(){ Parent.call(this) } Child.prototype=Parent.prototype;//缺点:因为原型上有个属性constructor,子类的实例constructor也是parent,无法区分实例对象是父类还是子类,子类原型上添加属性影响父类
寄生继承: 优点可以添加方法和属性
1 2 3 4 5 6
function createChild(p){ var o=Object.create(p); o.newMethod=function(){ } return o; }
寄生组合继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function object(o) { function F() {} F.prototype = o; return new F(); } function inheritPrototype(subClass,superClass){ var p=object(superClass.prototype); p.constructor=subClass; subClass.prototype=p; } function Child(){ Parent.apply(this,arguments); } inheritPrototype(Child, Parent);
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Parent = function () { function Parent(name, age) { _classCallCheck(this, Parent);
this.name = name; this.age = age; }
_createClass(Parent, [{ key: "speakSomething", value: function speakSomething() { console.log("I can speek chinese"); } }]);
var _createClass = function () { functiondefineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value"in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
returnfunction (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _possibleConstructorReturn(self, call) { if (!self) { thrownewReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { thrownewTypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { thrownewTypeError("Cannot call a class as a function"); } }
var Parent = function () { functionParent(name, age) { _classCallCheck(this, Parent);