简体   繁体   English

如何调用“定义”内部的JavaScript函数 - 匿名方法

[英]How to call a JavaScript function that is inside a “define” - anonymous method

How to call a function which is defined inside an anonymous function but both in the same JS file. 如何在同一个JS文件中调用在匿名函数内定义的函数。 Here is my code snippet. 这是我的代码片段。 How to call _testMethodInside() from testMethodOutside() ? 如何调用_testMethodInside()testMethodOutside()

// Line 1 to 13 is an existing code from ESRI API
    define([
        "dojo/_base/declare",
        "dojo/_base/html"
    ], function (
        declare,
        html
    ) {
        return declare([_WidgetBase, _TemplatedMixin], {
            _testMethodInside: function () {
                return 'success';
            }
        });
    });

//Call above using this function
    function testMethodOutside(){
        //How to call _testMethodInside() function from here
    }

Follow the Dojo documentation. 按照Dojo文档。 The define block defines a module. define块定义了一个模块。 You did not specify the module id (which would either be passed explicitly or inferred from the file name), so I will proceed as if the module is named my/Example . 您没有指定模块ID(可以显式传递或从文件名推断),因此我将继续进行,就像模块名为my/Example

require(['my/Example'], function(Example) {
   var example = new Example();
   example._testMethodInside(); // here is where you call _testMethodInside
}

The key thing is that because the module is loaded asynchronously, the only place you can safely call it from is the callback function you pass into (AMD) require . 关键是因为模块是异步加载的,所以你可以安全地调用它的唯一地方是你传入的回调函数(AMD) require

With esri's Web app builder you'd usually either : 使用esri的Web应用程序构建器,您通常可以:

1) Have all your code inside the define/require 2) Separate it into two modules 1)将所有代码放在define / require中2)将其分成两个模块

That's just how the design of the flow is supposed to go, for example : 这就是流程的设计应该如何进行,例如:

file1.js : file1.js:

define([
    "dojo/_base/declare",
    "dojo/_base/html"
], function (
    declare,
    html
) {
    return declare([_WidgetBase, _TemplatedMixin], {
        _testMethodInside: function () {
            return 'success';
        }
    });
});

file2.js : file2.js:

  require([
        './file1'
  ], function (File1) {

      File1._testMethodInside();
  })

Also, method names starting with an underline is a common design choice to designate private methods, so _testMethodInside should only really be called by file1 此外,以下划线开头的方法名称是指定私有方法的常见设计选择,因此_testMethodInside应该只由file1调用

If it should be just a common function for the _testMethodInside method and testMethodOutside function, consider the following: 如果它应该只是_testMethodInside方法和testMethodOutside函数的testMethodOutside函数,请考虑以下内容:

function sharedFunction() {
    return 'success';
}

function testMethodOutside() {
    sharedFunction();
}

define([
    "dojo/_base/declare",
    "dojo/_base/html"
], function (declare, html) {
    return declare([_WidgetBase, _TemplatedMixin], {
        _testMethodInside: sharedFunction
    });
});

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

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