简体   繁体   English

在firefox扩展中使用OOP

[英]Using OOP in firefox extension

I'm using classic OOP code from Mozilla 我正在使用Mozilla的经典OOP代码

var myExtension = {
    init: function() {
        // The event can be DOMContentLoaded, pageshow, pagehide, <b style="color:black;background-color:#99ff99">load</b> or unload.
        if(gBrowser) gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, false);
    },
    onPageLoad: function(aEvent) {
        var doc = aEvent.originalTarget; // doc is document that triggered the event
        var win = doc.defaultView; // win is the window for the doc
          alert("<b style="color:black;background-color:#a0ffff">page</b> is loaded \n" +doc.location.href);
        //OOPs, error here, username is undefined
        Firefox.Console.log(this.userName);
    },
    anotherMethod: function (){
       this.userName = 'UserName'; 
    }
}
window.addEventListener("<b style="color:black;background-color:#99ff99">load</b>", function() { myExtension.init(); }, false);

The general question is how can initialize and user public variables in my class? 一般的问题是如何在类中初始化和用户公共变量? You know, this in this class is some browser's object (tab), but not a current class and I can't assign this.win = doc.defaultView and use later like this.win.userName = 'UserName' 要知道, 在这个类是一些浏览器的对象(标签),而不是当前的阶级,我不能指定this.win = doc.defaultView和使用后像this.win.userName = 'UserName'

A couple of options: 有两个选择:

Using Function#bind : 使用Function#bind

I believe in a Firefox extension you should have most if not all ES5 features available to you, which means you can use Function#bind to make sure your event handler gets called with this set to your instance. 我相信,在Firefox扩展中,您应该可以使用大多数(如果不是全部)ES5功能,这意味着您可以使用Function#bind来确保使用this设置调用实例的事件处理程序。 Eg: 例如:

if (gBrowser) {
    gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad.bind(this), false);
}

Within the call to onPageLoad , this will refer to your instance. 在对onPageLoad的调用内, this将引用您的实例。 You won't have access to the normal this Firefox gives the event handler (the tab or whatever). 您将无法访问this Firefox提供的事件处理程序(选项卡或任何其他内容)的常规名称。

Fuller example of the bind option: bind选项的完整示例:

var myExtension = {
    init: function() {
        // The event can be DOMContentLoaded, pageshow, pagehide, <b style="color:black;background-color:#99ff99">load</b> or unload.
        if(gBrowser) {
            gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad.bind(this), false);
        }
    },
    onPageLoad: function(aEvent) {
        var doc = aEvent.originalTarget; // doc is document that triggered the event
        var win = doc.defaultView; // win is the window for the doc
          alert("<b style="color:black;background-color:#a0ffff">page</b> is loaded \n" +doc.location.href);
        // This now works
        Firefox.Console.log(this.userName);
        // ...but you don't have access to the `this` that the browser gave
        // the event handler (the tab or whatever)
    },
    anotherMethod: function (){
       this.userName = 'UserName'; 
    }
}
window.addEventListener("<b style="color:black;background-color:#99ff99">load</b>", function() { myExtension.init(); }, false);

Using a closure: 使用闭包:

If I'm mistaken about ES5 in Firefox extensions, or if you want to have access to the this that the event handler would normally receive, you can readily use a closure to do the same thing: 如果我在Firefox扩展中误认为ES5,或者想要访问事件处理程序通常会收到的this ,则可以轻松地使用闭包来执行相同的操作:

var self = this;
if (gBrowser) {
    gBrowser.addEventListener("DOMContentLoaded", function(event) {
        // Here, `self` refers to your instance, and `this` to the
        // element, so for instance you can call your `onPageLoad`
        // and pass it the element as a second argument:
        return self.onPageLoad(event, this);
        // Within the call to `onPageLoad`, `this` will be your instance
    }, false);
}

(If "closure" is a new term to you, don't worry, closures are not complicated .) (如果“ closure”是您的新名词,请放心, closure并不复杂 。)

Fuller example of the closure option: 关闭选项的完整示例:

var myExtension = {
    init: function() {
        var self = this;

        // The event can be DOMContentLoaded, pageshow, pagehide, <b style="color:black;background-color:#99ff99">load</b> or unload.
        if(gBrowser) {
            gBrowser.addEventListener("DOMContentLoaded", function(event) {
                return self.onPageLoad(event, this);
            }, false);
        }
    },
    onPageLoad: function(aEvent, aElement) {
        var doc = aEvent.originalTarget; // doc is document that triggered the event
        var win = doc.defaultView; // win is the window for the doc
          alert("<b style="color:black;background-color:#a0ffff">page</b> is loaded \n" +doc.location.href);
        // This now works
        Firefox.Console.log(this.userName);

        // If you needed the `this` that Firefox gives the event handler
        // (the tab or whatever), it's accessible via `aElement`
    },
    anotherMethod: function (){
       this.userName = 'UserName'; 
    }
}
window.addEventListener("<b style="color:black;background-color:#99ff99">load</b>", function() { myExtension.init(); }, false);

You might be hung up a little too much on using "this." 使用“ this”可能会使您过于烦恼。 Make your global variables properties of MyExtension and refer to variables by their full names rather than using "this," which can be ambiguous. 将MyExtension的全局变量属性设置为变量,并使用变量的全名来引用变量,而不要使用可能不明确的“ this”。 For example: 例如:

var myExtension = {
    username : '',
    email : '',

    init : function() {
        if ((app = document.getElementById("appcontent"))) {
            app.addEventListener("DOMContentLoaded", myExtension.run, true);
        }
   },

   run : function(event) {
       ...
    },

   anotherMethod : function() {
       // same as your example: this.username = 'Username';
       myExtension.username = 'Username';
   }

}; };

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

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