1

I'm building an application using RequireJS, and I need a centralized logger. This boils down to class structure, but I'm including my example to be specific. How can a subclass call a parent's error logging method?

Here is my app script:

define(['jquery', 'js/renderer', 'js/logger'], function($, Renderer, Logger) {
  var App = Class.extend({
    init: function(baseUrl) {
      this.log = new Logger();
      this.renderer = new Renderer();
    }
  });
  return App;
});

Here is the renderer script:

define(['jquery'], function ($) {
  try() {
    var Renderer = Class.extend({
      init: function() {
        vor = {}; // bad script
      }
    });
    return Renderer;
  } catch(e) {
    [How to throw the error to App]
  }
});

Is it too abstract to put the try/catch around the class definition? Should I only put it around instructions? For example:

define(['jquery'], function ($) {
    var Renderer = Class.extend({
      init: function() {
        try() {
          vor = {}; // bad script
        } catch(e) {
           [How to throw the error to App]
        }
      }
    });
    return Renderer;
});

One option I'm exploring is passing the app class as an argument for the renderer init, but is that bad practice? For example, updating the app script to use:

this.renderer = new Renderer(this);

Then in the renderer script I'd have this:

....
init: function(app) {
   app.log.error({message:"My Error"});
4

0 回答 0