Add prototype to object

I have a basic function that I want to inherit from:

function Base() {
    this._dummy = 1;
};
Base.prototype.notify = function () {
    alert("Notify");
};

Creating another constructor class works:

function Concrete1() {};
Concrete1.prototype = new Base();
var concr1 = new Concrete1();
concr1.notify(); // alerts "Notify"

Declaring an object like this does not work, perhaps because the object does not have a prototype

var concrete2 = new function () {};
concrete2.prototype = new Base();
concrete2.notify(); // doesn't work

I do not want to create a constructor Concrete2, because it will be only one instance of it.

Can I do "inheritance" using a method new function()?

+4
source share
2 answers

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(); // Works

concrete2, :

concrete2.doSomethingElse = function() { /* ... */ };

, -, , , - new.

, , , , . ( ), Object.create, ES5 :

// Creates an object backed by Base.prototype without calling Base
var concrete2 = Object.create(Base.prototype);
concrete2.notify(); // Works

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();                    // Not ideal

: , 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); // For example
    this.args = args;                // For example
}

:

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName  = lastName;
}
Person.prototype.getFullName = function() {
    return this.firstName + " " + this.lastName;
};
/* ...further Person-specific functions put on Person.prototype here...*/

function Employee(firstName, lastName, jobTitle) {
    Person.call(this, firstName, lastName);
    this.jobTitle = jobTitle;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
/* ...Employee-specific functions put on Employee.prototype here...*/

function Manager(firstName, lastName, jobTitle) {
    Employee.call(this, firstName, lastName);
    this.jobTitle = jobTitle;
}
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.constructor = Manager;
/* ...Manager-specific functions put on Manager.prototype here...*/

... . ( script, Lineage script, .)

+2

:

  • concrete1 new .
  • , concrete2.notify(), , . concrete2.prototype.notify() , new concrete2().notify().

JSFiddle ( ).

/Rewrite

@T.J.Crowder , @RaraituL , .

Base , /. , , :

function Base() {
    this._dummy = 1;
};

Base.prototype.notify = function () {
    alert("Notify");
};

Concrete1 = new Base();

//My Extension of Base
Concrete1.foo = function() {
    //Some method
}

, , "" , . , , .

+1

Source: https://habr.com/ru/post/1534369/


All Articles