Sign in to follow this  
misfit

Inheritance(?) in js question

Recommended Posts

Hi, I have been going through a tutorial on recreating FruitNinja (unimportant maybe). I am a beginner in JS with experiance in Java. I need some help or reference where to get it - I have 2 states like this:

var FruitNinja = FruitNinja || {};

FruitNinja.JSONLevelState = function () {
    "use strict";
    Phaser.State.call(this);
};

FruitNinja.JSONLevelState.prototype = Object.create(Phaser.State.prototype);
FruitNinja.JSONLevelState.prototype.constructor = FruitNinja.JSONLevelState;
(...)

FruitNinja.JSONLevelState.prototype.create = function () {
     this.create_prefab(prefab_name, this.level_data.prefabs[prefab_name]);

};

FruitNinja.JSONLevelState.prototype.create_prefab = function (prefab_name, prefab_data) {
    if (this.prefab_classes.hasOwnProperty(prefab_data.type)) { do stuff...
};

(...)

and

var FruitNinja = FruitNinja || {};

FruitNinja.LevelState = function () {
    "use strict";
    FruitNinja.JSONLevelState.call(this);
    this.prefab_classes = {
        "background": FruitNinja.Prefab.prototype.constructor,
        (...)
    };
};

FruitNinja.LevelState.prototype = Object.create(FruitNinja.JSONLevelState.prototype);
FruitNinja.LevelState.prototype.constructor = FruitNinja.LevelState;

FruitNinja.LevelState.prototype.create = function () {
    "use strict";
    FruitNinja.JSONLevelState.prototype.create.call(this);

(...)
}

 

Somehow JSONLevelState can access LevelState this.prefab_classe object. I don't understand how. Can I somehow print it to chrome console? Or can someone explain it or point me to some external reference ? I have watched couple of videos on youtube on inheritance in JS but I still don't get it.

Please help,

 

 

Ok Solved. "FruitNinja.JSONLevelState.call(this);" allows dunc from JSONLevelState be called in LevelState object (having access to its variables. Watch https://youtu.be/PMfcsYzj-9M for a complete explanation. 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.