经典类原型继承
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了经典类原型继承相关的知识,希望对你有一定的参考价值。
/** * Extends a sub class from a super class. * @param {Function} subClass The subclass. * @param {Function} superClass The super class to extend. */ function extends(subClass, superClass) { var midClass = function() { }; midClass.prototype = superClass.prototype; subClass.prototype = new midClass(); subClass.superClass = superClass; } /** * A person. * @constructor * @param {string} name */ function Person(name) { /** * @private {string} */ this._name = name; } /** * @return {string} */ Person.prototype.getName = function() { return this._name; }; /** * A user. * @constructor * @inheritDoc * @extends {Person} */ function User(name) { User.superClass.call(this, name); } extends(User, Person); /** * @return {number} */ User.prototype.getNameLength = function() { return this.getName().length; }; // Demo var user = new User('John Doe'); user.alertName(); // John Doe document.writeln(user.getName()); // John Doe document.writeln(user instanceof User); // true; document.writeln(user instanceof Person); // true;
以上是关于经典类原型继承的主要内容,如果未能解决你的问题,请参考以下文章