0

I've written the following "class" in javascript:

var On = {

    Request: {
        User: function() {
            _Login = function() {console.log('Let me see.')};
        }
    },
    User: function() {},
    Article: function() {},
    Comment: function() {}

};


On.User.prototype = {

    constructor: function() { On.Request.User.call(this)},

    Create: function(username, password, email) { 
        username = username || null;
        password = password || null;
        email = email || null;

        //here I want to call _Login from Request.user
    },

    Login: function(username, password) { 
        username = username || null;
        password = password || null;
    },

    Reminder: function(username, email) { 
        username = username || null;
        email = email || null;
    }
};

...

var x = new On.User();
x.Create();

How could I inherit in User function from Request.User? I've tried it in many different ways but can't make it work (even made a constructor in User's prototype). How could I make it possible to inherit, and call _Login from Login?

Ps.: this code is just an example, I'm newbie to prototype, and trying to learn it via examples I write.

Kiss Koppány
  • 919
  • 5
  • 15
  • 33
  • 4
    With so many articles and posts around the web... – DontVoteMeDown Apr 28 '15 at 12:45
  • [This-Inheritance and the prototype chain](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) is a good place to get started. [This-Javascript “OOP” and prototypes with multiple-level inheritance](http://stackoverflow.com/questions/15040955/javascript-oop-and-prototypes-with-multiple-level-inheritance?rq=1) might help break it into more understandable terms. – JasonWilczak Apr 28 '15 at 12:47
  • @DontVoteMeDown not to mention on this very site... – Jared Smith Apr 28 '15 at 12:47
  • http://prntscr.com/6z4mea Come on. – briosheje Apr 28 '15 at 12:58
  • @briosheje Note that that link appeared when the 2nd comment was posted and linked to it, ie it wasn't there when the 1st comment was written. – James Thorpe Apr 28 '15 at 13:00

1 Answers1

0

You should read proper JS material to get into the mindset of prototypal inheritance. Follow this list or this article for example.

Your code would look basically like this:

var On = {
  Request: {
    User: function requestUser() { console.log('let me see');}
  },
  User: function user() {}
}

On.User.prototype = On.Request.User();

On.User();

But be aware: This is not proper JS or how you would structure your app. More of an idea of what you can do.

You can play around with this code at JSBin.

ohboy21
  • 4,259
  • 8
  • 38
  • 66