简体   繁体   中英

How to create Google Closure like structure with inheritance

I wanted to created a Google Closure like structure where "classes" are stored in an "app.classes.someClass" fashion, so as to keep them under one namespace. Also I needed inheritance, with a parent method that would know it's current constructor name and generate a string according to the current (child) class. I managed to accomplish what I wanted, but I'm not sure this is the best possible solution for the inheritance to work how I wanted.

app.utils.inherits = function(childCtor, parentCtor)
{
  /** @constructor */
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  /** @override */
  childCtor.prototype.constructor = childCtor;
};   

app.classes.pageController = function pageController() {};
app.classes.pageController.prototype.init = function () {};
app.classes.pageController.prototype.getTemplate = function ()
{
  var name = this.constructor.name;
  name = name.replace('PageController', '');

  return $('#page-template-'+name).html();
};

// REQUEST PAGE CONTROLLER
app.classes.requestPageController = function requestPageController()
{
  app.classes.pageController.call(this);
};
app.utils.inherits(app.classes.requestPageController, app.classes.pageController);
app.classes.requestPageController.prototype.init = function ()
{
  var template = app.classes.pageController.prototype.getTemplate.call(this);
  ....
};

I guess the MVC framework 'ThinkMVC' implements the same pattern. https://github.com/chennanfei/ThinkMVC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM