Since it Baseis a design function, if you want one object to be used Base.prototypeas a base, and Brian Peacock points out below , you can just use the constructor:
var concrete2 = new Base();
concrete2.notify();
concrete2, :
concrete2.doSomethingElse = function() { };
, -, , , - new.
, , , , . ( ), Object.create, ES5 :
var concrete2 = Object.create(Base.prototype);
concrete2.notify();
Object.create , , . , , :
if (!Object.create) {
Object.create = function(proto, props) {
if (typeof props !== "undefined") {
throw "The two-argument version of Object.create cannot be shimmed.";
}
function ctor() { }
ctor.prototype = proto;
return new ctor();
};
}
, , , Concrete1 Base. :
Concrete1.prototype = new Base();
: , Base - ? , , , ? ( , Base .) Base "". Object.create:
Concrete1.prototype = Object.create(Base.prototype); // Better
... :
Concrete1.prototype.constructor = Concrete1;
... , ( , constructor , constructor -).
Base Concrete1 :
function Concrete1(any, necessary, args) {
Base.call(this, any, necessary);
this.args = args;
}
:
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.getFullName = function() {
return this.firstName + " " + this.lastName;
};
function Employee(firstName, lastName, jobTitle) {
Person.call(this, firstName, lastName);
this.jobTitle = jobTitle;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
function Manager(firstName, lastName, jobTitle) {
Employee.call(this, firstName, lastName);
this.jobTitle = jobTitle;
}
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.constructor = Manager;
... . ( script, Lineage script, .)