简体   繁体   English

扩展在JavaScript通用模块(UMD文件)中定义的类的正确方法是什么?

[英]What is the correct way to extend a class defined in a JavaScript Universal Module (UMD file)

I've made a first attempt at a custom log "appender" for log4javascript . 我已经首次尝试为log4javascript创建自定义日志“ appender”。 It works, but it is stuck inside the original code. 它可以工作,但是卡在原始代码中。 I want to keep my extension in a separate file, but I don't understand the rest code well enough to know how to extract it. 我想将扩展名保存在一个单独的文件中,但是我对其余代码的了解不够深,无法知道如何提取它。

I understand that log4javascript respects the UMD pattern somehow, but that's as far as I have got. 我了解到log4javascript某种程度上尊重UMD模式,但据我所知。

(Although shouldn't it be... (虽然不应该...

(function(root, factory){})(this, function{})... (function(root,factory){})(this,function {})...

... instead of ... ... 代替 ...

(function(factory, root){})(function{}) (函数(工厂,根){})(函数{})

... ????*) ... ???? *)

(function(factory, root) {
    if (typeof define == "function" && define.amd) {
        define(factory);
    }
    else if (typeof module != "undefined" && typeof exports == "object") {
        module.exports = factory();
    }
    else {
        root.log4javascript = factory();
    }
})(function() {

        :
  Here be dragons . . .
        :


    /*  -- -  this code works from here, but not if I    - -- */
    /*  -- -  try to move it out to a file of its own    - -- */    
    function MyAppender() {}
    MyAppender.prototype = new Appender();
    MyAppender.prototype.layout = new SimpleLayout();
    MyAppender.prototype.append = function(loggingEvent) {
      alert( this.getLayout().formatWithException(loggingEvent) );
    };
    MyAppender.prototype.toString = function() {
      return "MyAppender";
    };
    log4javascript.MyAppender = MyAppender;
    /*  -- -      -   -   -   -   -   -   -   -   -      - -- */    



        :
 . . . and sea monsters
        :


    return log4javascript;
}, this);

How can I subclass Appender in a separate file? 如何将Appender放在单独的文件中?

Since posting a few hours ago, I've been reading more about UMD, AMD, requireJS, etc. 自几个小时前发布以来,我一直在阅读有关UMD,AMD,requireJS等的更多信息。

The main issue is name space purity, it seems. 似乎主要的问题是名称空间的纯度。

Having figured that out, all I had to do was solve the name space problem. 弄清楚了之后,我所要做的就是解决名称空间问题。 It turns out to be stupidly easy: it's right there in the code! 事实证明这很容易:代码就在其中! Duh! 咄! :

log4javascript.MyAppender = MyAppender; log4javascript.MyAppender = MyAppender;

Instead of . 代替 。 . .

MyAppender.prototype = new Appender();
MyAppender.prototype.layout = new SimpleLayout();

... I just had to do ... ...我只需要做...

MyAppender.prototype = new log4javascript.Appender();
MyAppender.prototype.layout = new log4javascript.SimpleLayout();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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