简体   繁体   English

如何在该匿名javascript中调用该函数? (TinyMce示例)

[英]How can i call that function inside that anonymous javascript? (TinyMce example)

How can i call test() inside that method? 如何在该方法内调用test()? It's possible? 这是可能的?

(function() {

    tinymce.create('tinymce.plugins.WrImagerPlugin', {

        init : function(editor, url) { 

            editor.addCommand('mceWrImagerLink', function() {
                //--> how can i refer to test() here?
            });
        },
        test: function () {alert('test');}
        }
    });

    tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);
})();

You can make test a regular function and assign it to the object, like this: 您可以使test成为常规函数并将其分配给对象,如下所示:

(function() {
    function test() { alert('test'); }

    tinymce.create('tinymce.plugins.WrImagerPlugin', {
        init : function(editor, url) { 
            editor.addCommand('mceWrImagerLink', function() {
                test();
            });
        },
        test: test
    });

    tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);
})();

Alternatively, you can keep a reference to the object: 或者,您可以保留对该对象的引用:

(function() {
    var wrImaergPlugin = {    
        init : function(editor, url) { 
            editor.addCommand('mceWrImagerLink', function() {
                wrImagerPlugin.test();
            });
        },
        test: function() { alert('test'); }
    }

    tinymce.create('tinymce.plugins.WrImagerPlugin', wrImagerPlugin);
    tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);
})();

Finally, in this specific case, you should be able to simply call tinymce.plugins.WrImagerPlugin.test() . 最后,在这种特定情况下,您应该能够简单地调用tinymce.plugins.WrImagerPlugin.test()

You can also keep a reference to this in the init method that will be available in the addCommand closure: 您还可以在init方法中保留this的引用, this方法将在addCommand闭包中提供:

(function() {

tinymce.create('tinymce.plugins.WrImagerPlugin', {

    init : function(editor, url) { 
        var me = this;
        editor.addCommand('mceWrImagerLink', function() {
            //--> how can i refer to test() here?
            me.test();
        });
    },
    test: function () {alert('test');}
    }
});

tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);

})();

暂无
暂无

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

相关问题 如何在JavaScript中从内部调用匿名函数? - How can I call an anonymous function from inside itself in JavaScript? 如何在javascript匿名函数中调用类释放函数 - How can I call the class slibing function in javascript anonymous function 如何在JavaScript中使用参数调用匿名函数(存储在字符串中)? - How can I call an anonymous function (stored in string) with an argument in JavaScript? 如何显示(或调用)在javascript中的数组内部定义的匿名函数的内容 - how do I display(or call) the contents of an anonymous function that is defined inside of an array in javascript 如何调用“定义”内部的JavaScript函数 - 匿名方法 - How to call a JavaScript function that is inside a “define” - anonymous method 如何在cordova函数中调用有角JavaScript函数 - How can I call a angular JavaScript function inside a cordova function 我可以在此示例中立即调用函数,而不用匿名函数将其包装吗? - Can I call my function in this example immediately instead of wrap it by anonymous function? 如何将匿名函数内的变量导出到JavaScript中的全局作用域? - How can I export a variable inside of an anonymous function to the global scope in JavaScript? 如何在Javascript中正确调用匿名函数? - How do I correctly call a functions of anonymous function in Javascript? 如何取消javascript中间隔内的函数调用? - How can I cancel a function call that's inside an interval in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM