JavaScript layered inheritance

I am trying to find JavaScript inheritance. Vaguely, it seems that there are many different approaches - Crockford presents a lot of these, but can’t just look into his prose (or maybe just not related to my specific scenario).

Here is an example of what I still have:

// base class
var Item = function( type, name ) {
    this.type = type;
    this.name = name; // unused
};

// actual class (one of many related alternatives)
var Book = function( title, author ) {
    this.name = title; // redundant (base class)
    this.author = author;
};
Book.prototype = new Item('book'); // duplication of "book"

// instances
var book = new Book('Hello World', 'A. Noob');

This approach leaves me with enough redundancy because I cannot delegate instance-specific attributes to the base class (the attribute value is unknown at the time the prototype is assigned). Thus, each subclass must repeat this attribute. Is there a recommended way to solve this problem?

Bonus question: is there a reasonable way to avoid the “new” operator, or will it be considered a novice working against the language?

+5
4

, : -

 function Item(type, name)
 {
    if (arguments.length > 0)
    {
        this.type = type;
        this.name = name;
    }
 }

 function Book(title, author)
 {
   Item.call(this, "book", title);
   this.author = author;
 }
 Book.prototype = new Item();

, , , , , .

, , Item.call . , .

new, , , ? , " " , : -

Book.Create = function (title, author) { return new Book(title, author); }


var aBook = Book.Create("Code Complete 2", "Steve McConnell");

.

+3

""

, JavaScript Item.prototype . Item, : Item, .

new , , Item . , , , :

function Item(type) {
    this.type= type;
};

function Book(title, author) {
    Item.call(this, 'book');
    this.name= title;
    this.author = author;
};
function Item_nonconstructor() {}
Item_nonconstructor.prototype= Item.prototype;
Book.prototype= new Item_nonconstructor();

, , :

Function.prototype.subclass= function(base) {
    var c= Function.prototype.subclass.nonconstructor;
    c.prototype= base.prototype;
    this.prototype= new c();
};
Function.prototype.subclass.nonconstructor= function() {};

function Book(title, author) {
    Item.call(this, 'book');
    this.name= title;
    this.author = author;
};
Book.subclass(Item);

. JavaScript /. . JavaScript.

+3

, ( jQuery) , , , OO: JavaScript. ( , ), .

, , , ... . , - - javascript.

0

:

//The functions we are going to use 
function A() {}
function B() {}
function C() {}
function D() {}
function E() {}

//set up the inheritance chain (order is important!)
D.prototype = new E();
C.prototype = new D();
B.prototype = new C();
A.prototype = new B();

//Add custom functions to each
A.prototype.foo = function() {
    console.log("a");
};
B.prototype.bar = function() {
    console.log("b");
};
C.prototype.baz = function() {
    console.log("c");
};
D.prototype.wee = function() {
    console.log("d");
};
E.prototype.woo = function() {
    console.log("e");
};

//Some tests
a = new A();
a.foo();
a.bar();
a.baz();
a.wee();
a.woo();

, . javascript" , , , , ..

0

All Articles