2

In my spare time I try to learn a little bit of JS, but I stuck with the topic in subject.

var person = new Person("Bob", "Smith", 52);
var teacher = new Teacher("Adam", "Greff", 209);

function Humans(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

function Person(firstName, lastName, age) {
  Humans.call(this, firstName, lastName);
  this.age = age;
}

Person.prototype = Object.create(Humans.prototype);

Person.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.age;
};


function Teacher(firstName, lastName, roomNumber) {
  Humans.call(this, firstName, lastName);
  this.room = roomNumber;
}

Teacher.prototype = Object.create(Humans.prototype);

Teacher.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.room;
};

person.fullDetail();

Can anybody tell me why I cant execute person.fullDetail();?

If you could make some comments with your version of code, I would be very grateful, thanks.

6
  • 3
    you are creating the instances before defining the functions! Commented Nov 14, 2016 at 21:20
  • @DanielA.White Hoisting fixes some of this Commented Nov 14, 2016 at 21:21
  • Hoisting only applies to a few of the functions here. Commented Nov 14, 2016 at 21:22
  • @Feathercrown Not the issue of setting the wrong prototype, which is exactly the problem Commented Nov 14, 2016 at 21:25
  • @Bergi yeah, on second thought, it doesn't fix the whole thing. Commented Nov 14, 2016 at 21:26

4 Answers 4

6

Because you're creating your objects before you've defined what their prototypes should be.

When you do

var person = new Person ("Bob", "Smith", 52);

you're making an object based on the current definition of Person. Later in that code, you're changing the prototype of Person in it's entirety

Person.prototype = Object.create(Humans.prototype);

To fix this, create your objects after you're done re-assigning the prototype.

function Humans(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

function Person(firstName, lastName, age) {
  Humans.call(this, firstName, lastName);
  this.age = age;
}

Person.prototype = Object.create(Humans.prototype);

Person.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.age;
};


function Teacher(firstName, lastName, roomNumber) {
  Humans.call(this, firstName, lastName);
  this.room = roomNumber;
}

Teacher.prototype = Object.create(Humans.prototype);

Teacher.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.room;
};

var person = new Person("Bob", "Smith", 52);
var teacher = new Teacher("Adam", "Greff", 209);
console.log(person.fullDetail());

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

1 Comment

You can actually prove it by showing that Object.getPrototypeOf(person) !== Person.prototype. The prototype or the original is person instance is "lost" and replaced. Very interesting edge case for javascript inheritance!
5

Yes it is because when you are creating person object Person prototype has no FullDetail method.

Change the ordering of your object creation ,create person object after adding methods to prototype

check this snippet

var teacher;
var person;
function Humans(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

function Person(firstName, lastName, age) {
  Humans.call(this, firstName, lastName);
  this.age = age;
}

Person.prototype = Object.create(Humans.prototype);

Person.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.age;
};

 person = new Person("Bob", "Smith", 52);

function Teacher(firstName, lastName, roomNumber) {
  Humans.call(this, firstName, lastName);
  this.room = roomNumber;
}

Teacher.prototype = Object.create(Humans.prototype);

Teacher.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.room;
};
teacher= new Teacher("Adam", "Greff", 209);
console.log(person.fullDetail());
console.log(teacher.fullDetail());

Hope it helps

4 Comments

While this fixes that use case, it doesn't fix the fact that teacher is still broken!
Just to add, the reason you're able to create a person before the constructor function (and prototype changes) is because of this annoying/counter-intuitive thing called 'hoisting' developer.mozilla.org/en/docs/Web/JavaScript/Reference/….
@tcooc try moving the teacher instantiation down below the Teacher constructor
Your explanation isn't entirely correct. It does not matter when the methods are added to the prototype - it just needs to be done before calling them.
1

I think it's because you create the person and teacher without having their functions defined yet in the prototype. Try this:

    function Humans(firstName, lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
    }
    
    function Person(firstName, lastName, age) {
      Humans.call(this, firstName, lastName);
      this.age = age;
    }
    
    Person.prototype = Object.create(Humans.prototype);
    
    Person.prototype.fullDetail = function() {
      return this.firstName + " " + this.lastName + " " + this.age; 
    };
    
    
    function Teacher(firstName, lastName, roomNumber) {
      Humans.call(this, firstName, lastName);
      this.room = roomNumber;
    }
    
    Teacher.prototype = Object.create(Humans.prototype);
    
    Teacher.prototype.fullDetail = function() {
      return this.firstName + " " + this.lastName + " " + this.room; 
    };
    var person = new Person ("Bob", "Smith", 52);
    var teacher = new Teacher ("Adam", "Greff", 209);
    
    console.log(person.fullDetail());
( ͡° ͜ʖ ͡°)

Comments

0

If you are at ES6, you could use the new class syntax like

class ExampleBaseClass {
    // Do things here...
}

class ExampleClass extends ExampleBaseClass {
    // Do other things here...
    // You may use methods from ExampleBaseClass here.
}

See: https://www.sitepoint.com/understanding-ecmascript-6-class-inheritance/

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.