Search code examples
javascriptclosuresphaser-framework

How to access the internal function?


var $jscomp = $jscomp || {};
$jscomp.scope = {};
$jscomp.ASSUME_ES5 = !1;
$jscomp.ASSUME_NO_NATIVE_MAP = !1;
$jscomp.ASSUME_NO_NATIVE_SET = !1;
$jscomp.SIMPLE_FROUND_POLYFILL = !1;
$jscomp.objectCreate =
  $jscomp.ASSUME_ES5 || "function" == typeof Object.create
    ? Object.create
    : function (e) {
        var m = function () {};
        m.prototype = e;
        return new m();
      };
$jscomp.underscoreProtoCanBeSet = function () {
  var e = { a: !0 },
    m = {};
  try {
    return (m.__proto__ = e), m.a;
  } catch (w) {}
  return !1;
};
$jscomp.setPrototypeOf =
  "function" == typeof Object.setPrototypeOf
    ? Object.setPrototypeOf
    : $jscomp.underscoreProtoCanBeSet()
    ? function (e, m) {
        e.__proto__ = m;
        if (e.__proto__ !== m) throw new TypeError(e + " is not extensible");
        return e;
      }
    : null;
$jscomp.inherits = function (e, m) {
  e.prototype = $jscomp.objectCreate(m.prototype);
  e.prototype.constructor = e;
  if ($jscomp.setPrototypeOf) {
    var w = $jscomp.setPrototypeOf;
    w(e, m);
  } else
    for (w in m)
      if ("prototype" != w)
        if (Object.defineProperties) {
          var B = Object.getOwnPropertyDescriptor(m, w);
          B && Object.defineProperty(e, w, B);
        } else e[w] = m[w];
  e.superClass_ = m.prototype;
};

var Game = function () {
  return Phaser.Scene.call(this, "game") || this;
};
$jscomp.inherits(Game, Phaser.Scene);
Game.prototype.create = function () {
  this.someFunctionName = fn;
  function fn(a) {
    dosomething...
  }
}

var config = {
    type: Phaser.WEBGL,
    transparent: !0,
    width: 720,
    height: 1080,
    scale: {
      mode: Phaser.Scale.FIT,
      parent: "game_content",
      autoCenter: Phaser.Scale.CENTER_BOTH,
    },
    scene: [Boot, Load, Menu, Level, Game],
  },
game = new Phaser.Game(config);

game.create();
console.log(game.someFunctionName());

So how to access the function fn? So how to access the function fn?

Can't access the function fn! Got this error "caught TypeError: game.create is not a function". I just want to use the fn function outsider.

Can't access the function fn! Got this error "caught TypeError: game.create is not a function". I just want to use the fn function outsider.


Solution

  • Depending on your usecase you could simply attach the function to the current instance:

    ...
    var Game = function () {
        return Phaser.Scene.call(this, "game") || this;
    };
    
    $jscomp.inherits(Game, Phaser.Scene);
    
    Game.prototype.create = function () {
     
        function fn(a){
            ...
        }
    
        this.someFunctionName = fn; // <-- an easy solution, works in some cases
    }
    

    And than you can access the function through the someFunctionName method. (but you will need to call the create function on the instance, so that the function is bound to the instance)

    I removed the alternative Version, since after thinking about it it is a very bad way to solve this problem

    UPDATE:
    Disclaimer: This should work, but I don't know why you would ever want to do this.

    ...
    var game = new Phaser.Game(config);
    
    // the game is not created right await you need to wait, abit.
    setTimeout( ( ) => {
        console.info(game.scene.keys['game'].someFunctionName())
    }, 0);
    ...
    

    Info: the key game is due to the parameter passed, to this line of code return Phaser.Scene.call(this, "game") || this;. If this value would change you would have to adapt it, in both places.

    personally I would use ES6 inheritance to create Custom Scene this makes everything more readable. Checkout this example