4

I'm trying to understand how inheritance works in JS. Suppose we have a class:

Class = function () {
    this.A = 'A';
    this.B = 'B';
};

and we are trying to extend it

SubClass = function () {};
SubClass.prototype = new Class();

Do I understance correctly that after inheritance properties A and B are common for all instances of SubClass, since they belong to it's prototype? If yes, how can Class be extended so that A and B do not be part of prototype?

UPD: note that Class uses A and B, so I can't declare them in SubClass.

Thank you in advance!

5
  • 2
    how can Class be extended so that A and B do not be part of prototype? What's the purpose of using prototypes then? Commented Nov 14, 2012 at 9:37
  • If you want to have in each instance of SubClass 'A' and 'B' just don't declare it in prototype, declare it in SubClass. Commented Nov 14, 2012 at 9:42
  • @Prodigy,Class uses A and B, so I can't declare them in SubClass. Commented Nov 14, 2012 at 9:46
  • @elclanrs,I'm not sure I'm using the right terminology. All I want is to make A and B be accessible and specific for each "instance" of SubClass. Commented Nov 14, 2012 at 9:48
  • What exactly do you need to inherit from Class? Javascript is very flexible so you are likely just need another, maybe non-prototypal way of extending your objects. Commented Nov 14, 2012 at 9:48

2 Answers 2

3

All I want is to make A and B be accessible and specific for each "instance"

The typical way of doing this is to pass parameters and assign them to properties. Then you can use call to reference the super class. In other words:

function Person( name, age ) {
  this.name = name;
  this.age = age;
}

function Student( name, age, grade ) {
  Person.call( this, name, age ); // call super-class with sub-class properties
  this.grade = grade;
}

Student.prototype = new Person();
Student.prototype.constructor = Student;

var roger = new Student( 'Roger', 18, 'A+' );
Sign up to request clarification or add additional context in comments.

2 Comments

thank you! looks like you're proposing just what I need, the only question is: maybe there should be Person.call( this, name, age ); instead of Person.call( name, age );
I have just confirmed that it should, indeed, be Person.call(this, name, age). The this reference is essential.
1

You can use properties in parent class without defining:

Class = function () {
   this.sum = function() {
       return this.a+this.b;    
   }
};

SubClass = function () {
    this.a = 5;
    this.b = 6;
}; 

SubClass.prototype = new Class();

var z = new SubClass();
z.sum(); //11

Another way: Create function in prototype which creates your properties:

Class = function () {   
    this.makeAB = function() { //called with context of SubClass
        this.A = 'A';
        this.B = 'B';        
    }
};

SubClass = function () { this.makeAB() }; 
SubClass.prototype = new Class();

var z = new SubClass();
z.A = 'AAA';
z.B = 'BBB';

var z2 = new SubClass();

console.log(z)
console.log(z2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.