tags:

views:

53

answers:

2

In the following example, is there a way to construct the object such that “b” has a property a1, initialised to “2”?

function A(a1) {
    this.a1 = a1;
}

function B(b1, a1) {
    this.b1 = b1;
}

B.prototype = new A;

var b = new B('1', '2');

I am basically trying to duplicate what would be known as “calling the base constructor” in a traditional object orientated language (such as c#).

A: 

Like this?

function B(b1, a1) {
    A.call(this, a1);
    this.b1 = b1;
}
npup
That works. It will call A's constructor twice (once because of the line: B.prototype = new A;). I now wonder if I need the "B.prototype = new A;" line at all, since I never use instanceof. Thanks.
zod
A: 

Prototype is a javascript framework that helps you do class based programming. Have a look at their sources.

ormuriauga