2

I have:

Master object

function Fruit() {
    this.type = "fruit";
}

Sub-object:

function Bannana() {
    this.color = "yellow";
}

Inherit master properties

Bannana.prototype = Object.create( Fruit.prototype );

var myBanana = new Bannana();

console.log( myBanana.type );

Outputs: undefined. Why is this not displaying "fruit" as the outcome?

10
  • 2
    Bannana.prototype =Object.create( new Fruit ); would probably do what you want Commented Jun 22, 2015 at 22:46
  • 2
    @dandavis: It does, but it's not better than Bannana.prototype = new Fruit(); Commented Jun 22, 2015 at 22:49
  • why do not go usual way, like: "public class Bannana extends Fruit"? Commented Jun 22, 2015 at 22:51
  • @YevgeniyAfanasyev: i don't think that's the usual way since its not valid JS... Commented Jun 22, 2015 at 22:51
  • @dandavis: It's valid ES6/ES2015. Commented Jun 22, 2015 at 22:52

3 Answers 3

4

Why is this not displaying "fruit" as the outcome?

Because you are never setting type on the new object.

type isn't a property of Fruit.prototype, and all that Bannana.prototype = Object.create( Fruit.prototype ); does is make the properties of Fruit.prototype available to each Banana instance.

type is set by the Fruit function. But if you look at your code, nowhere are you executing Fruit! The line this.type = "fruit"; is never executed! The type property does not magically come to existence.

So in addition to setting the prototype, you have to execute Fruit. You have to call the parent constructor (just like you do in other languages (and ES6 now) via super):

function Bannana() {
    Fruit.call(this); // equivalent to `super()` in other languages
    this.color = "yellow";
}

In the new JavaScript version (ES6/ES2015) you would use classes instead:

class Banana extends Fruit {
    constructor() {
        super();
        this.color = 'yellow;
    }
}

This does the same thing, but hides it behind the class syntax for ease of use.

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

9 Comments

as shown, is there any use of actually duping the proto since all the interesting "inherited" props are own properties ?
@dandavis: Not in this case but in general yes.
Nicely done Felix. Totally understand it better now! Thanks!
@RobertRocha: Not really, it still creates the Banana.prototype object as you'd expect. It's mostly the looks.
@YevgeniyAfanasyev: I won't because I don't want to promote this style. See my answer here: stackoverflow.com/q/17392857/218196
|
0

This is so cool. If you go this way:

function Fruit() {
    this.type = "fruit";
}
function Bannana() {        
    this.color = "yellow";
}
Bannana.prototype =  new Fruit;
Bannana.prototype.type='flower';
var myBanana = new Bannana();
console.log( myBanana.type );

you will get a "flower", but if you go this way:

function Fruit() {
    this.type = "fruit";
}
function Bannana() {
    Fruit.call(this);
    this.color = "yellow";
}
Bannana.prototype.type='flower';
var myBanana = new Bannana();
console.log( myBanana.type );

You will get a "fruit";

I believe no explanation needed, right?

1 Comment

Don't know why fruit would be output. Have you created a question for this?
-3

You never put anything on the Fruit prototype object. Your constructor initializes the instances, not the prototype.

If you had:

Fruit.prototype.type = "fruit";

then your code would work as you expect.

5 Comments

I thought that Bannana.prototype = Object.create( Fruit.prototype ); would cause inheritance of the master object properties that already have values.
@RobertRocha it would. Your code does not put any properties on the Fruit prototype. The code in your Fruit constructor, to be specific, does not do that.
@RobertRocha: JavaScript is less magical then you seem to think.
Sorry I am new to JavaScript and trying to learn the ropes of prototypes and how they work. In lots of code on MDN I see it use the Object.create() function when extending master objects. For some reason Bannana.prototype = new Fruit() seems to do the trick.
They also aren't calling the Fruit constructor from the Bannana constructor.

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.