0

I just started to get into JS lately and I am using module pattern a lot and I really don't know if I am doing it right when it comes to doing inheritance from the module I wrote.

Here are the following .js files I am working on:

define(["src/Inhabitant"], function(Inhabitant)
{
  console.log("Coin.js loaded");

  return (function()
  {
    function Coin(stage , position)
    {
      Inhabitant.call(this, stage, position, "coinGold.png");
    }

    Coin.prototype = 
    {
        prototype   : Object.create(Inhabitant.prototype)
      , constructor : Coin
      , update      : update
    }

    function update(elapsed)
    {

    }

    return Coin;
  })();

});

I have a JS class named as Coin and its parent is Inhabitant:

define([], function()
{
  console.log("Inhabitant loaded.");

  return (function()
  {
    var mStage = null;
    var mSprite = null;
    var mID = -1;    

    function Inhabitant(stage, position , resource)
    {
      mStage = stage;
      mSprite = new PIXI.Sprite.fromFrame(resource);
      mSprite.position = position;
    }

    Inhabitant.prototype = 
    {
        constructor : Inhabitant

      , get position(){ return mSprite.position; } , set position(position){ mSprite.position = position; }
      , get x(){ return mSprite.x; } , set x(x){ mSprite.x = x; }
      , get y(){ return mSprite.y; } , set y(y){ mSprite.y = y; }

      , get id(){ return mID; } , set id(id){ return mID; }

      , get sprite(){ return mSprite; }

      , update : update
    }

    function update(elapsed)
    {
      console.log("Calling update from Inhabitant");
    }

    return Inhabitant;
  })();

});

I am stuck on this one because I can't even call the methods I am supposed to inherit. Even the update function isn't provided by the parent. If I remove the update from Coin it will not call the parent version (I don't know if I have the correct assumption on this one).

Also most of the time I write my classes this way

define([] , function()
{
    return function()
    {
        var o = {};

        return o
    }
});

This works most of the time since I am creating objects without the need of inheritance that much. But now I need to it in prototypal way so I can reduce code duplication.

What is the proper way of doing Module Pattern with prototypal inheritance given on what I currently have?

This have been asked many times via this link and this link but its not helping my situation.

Any ideas?

2
  • 1
    you can return factory method (or fabric object), and not all modules code. It kills your performance. Factory method can look like return { createCoin: function(params) { return new Coin(params) } }. You can call it in another file, to get you Coin object var coin = ModuleName.createCoin(params); or for inheritance function ChildObj(){}; ChildObj.prototype = new ModuleName.createCoin(params); Read more about AMD here - requirejs.org/docs/whyamd.html but note, that it's old things - benmccormick.org/2015/05/28/moving-past-requirejs Commented Nov 3, 2015 at 9:38
  • So what I am encountering like some of the attributes are undefined is because of the limitation/my misuse of Module Pattern? Am I correct? Commented Nov 3, 2015 at 12:09

0

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.