Results 1 to 3 of 3

Thread: Prototype Inheritance Question

Hybrid View

  1. #1
    Join Date
    Jun 2013

    Prototype Inheritance Question

    Could somebody please help me get my head around prototype inheritance I've done a lot of reading and not found something quite specific to what I am doing. Basically for a simplified example I have a game with levels, so I have a generic 'level' function that I want to base levels off.

    So I've written my code for that template level
    function level() {
    //generic level functionality
    Then I want to write a level1 function that inherits properties and methods from level but I don't want anything in the level constructor to execute until level1 is instantiated. So at the end I will have something like
    var RunLevel
    StartGameButton.onClick = function()
    {RunLevel = new level1();
    And I ASSUME, assuming wrong.. Is, level1 is something like,
    level1 = function() {
    this.prototype = new level();
    //level 1 functionality
    //Rather than this being outside the level1 function
    level1.prototype = new level();
    When I try the second I get everything in the level constructor executing at runtime which I don't want, I don't want it until the StartGameButton is hit and hence, level1 instantiated.

    If I'm going about this completely the wrong way let me know because I probably am. I've got a published project in Flash/AS3.0 that I'm trying to port to Javascript and the class to prototypes business is what's going to trip me up I think.

    Cheers for any help in advance,

  2. #2
    Join Date
    Mar 2011
    Setting the prototype within the constructor function isn't the right way to go, since it will set the prototype everytime an instance is created.
    To avoid execution of the 'level' constructor when you inheriting, you can set the 'level1' prototype by creating an object of the level constructor using
    Object.create(). Or if you need to support older browsers (if I'm right IE support it since version 9), you can use an intermediate constructor.

    function level(){};
    function level1(){};
    level1.prototype = Object.create(level);
    or the old way
    function level(){};
    function level1(){};
    function intermediate(){};
    intermediate.prototype = level.prototype;
    level.prototype = intermediate.prototype;
    level1.prototype.constructor = level1;		// reset the constructor
    the old way using a function
    var extend = (function extend(I){
     return function extend(A, B){
      I.prototype = B.prototype;
      A.prototype = new I;
      A.prototype.constructor = A;
    function Base(){};
    function Sub(){};
    extend(Base, Sub);

  3. #3
    Join Date
    Jun 2013
    Object.create was what I was after than I've chained constructor calls using templateobject.call(this). Thanks for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center