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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.