0

I am a beginner and can't understand how the prototypes and inheritance works in JavaScript. I am basing on the code here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance and I can't figure out how to inherit properties values, I can only get "methods". I think that maybe the more appropriate question is how to initiate fields of parent class when invoking a child object?

In accordance with the mentioned site, I wrote something like this:

    function Person(name, surname) {
        this.name = name;
        this.surname = surname;     
    } 
function Student(index) {
    this.index = index;
    Person.call(this);
}

Student.prototype = new Osoba ();//I tried to insert values here in the constructor, it doesn't work
Student.prototype.constructor = Student;

var x = new Student ('89890');//tried to insert additional values here in the constructor, it also doesn't work

Is there a way to create a student and give him also a name and surname?

I am a total noobie so please explain like you would explain to a 5 year old. PS. I have to do this in JS so please don't recommend different ways, it won't help me, thanks :)

1
  • Maybe the following answer can help you, there is also a pattern that deals with constructor parameters but you can use that in any function chain: stackoverflow.com/a/16063711/1641941 Commented Mar 31, 2014 at 2:17

3 Answers 3

1

The correct approach would be to have your child constructor repeat required parameters.

function Student(index, name, surname) {
    this.index = index;
    Person.call(this, name, surname);
}

var s = new Student ('89890', 'Jan', 'Kowalski');

Btw. this

Student.prototype = new Osoba ();

is certainly a typo, instead have

Student.prototype = new Person();

and you really don't need parameters here. The prototype will be initialized with undefined in property values and this is perfectly legal.

Sign up to request clarification or add additional context in comments.

5 Comments

@Bergi: I don't quite get this comment, even following the link. Can you elaborate? Taken "as is", this comment is wrong.
I'm saying that Student.prototype = new Person(); is wrong and should be Student.prototype = Object.create(Person.prototype);
@Bergi: agree. The confusion is your not new Person while you meant not new Person()
Yeah, the parenthesis can simply be omitted, I focused on the new :-)
0

You can change Student constructor to this one:

function Student(index, name, surname) {
    this.index = index;
    Person.call(this, name, surname);
}

call takes optional parameters so you can invoke Person with additional arguments to set name and surname own properties.

Comments

0
var inherit = function(C, P) {
      var F = function(){}; // i created this "proxy" to isolate the Parent constructor
      F.prototype = P.prototype;
      C.prototype = new F();
};

var Person = function(name, surname) {
       this.name = name || '';
       this.surname = surname || '';

       this.getName = function () {
         return this.name;
       }

      this.getSurname = function () {
         return this.surname;
      }
};


var Student = function(index, name, surname) {
  this.index = index;
  Person.call(this, name, surname);
};

inherit(Student, Person);

var student = new Student(0, 'Rodrigo', 'Fonseca');
alert(student.getName());
alert(student.getSurname());

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.