简体   繁体   English

如何让函数直接调用自身?

[英]How can I make a function directly call itself?

I have this function which I define and as soon as it is defined I execute it. 我有这个我定义的功能,一旦定义我执行它。 Is there a way I can combine the definition and the execution in one go as this is the only place this function is used? 有没有办法可以一次性结合定义和执行,因为这是唯一使用此功能的地方?

    $scope.examWatchCollection = function () {
        $scope.$watchCollection('[config.examStatusId, config.examTypeId]',
            function (newValue, oldValue) {
                if (_o.checkWatch(newValue, oldValue)) {
                    _u.putConfigs($scope.config);
                    $scope.grid.backup = [];
                    $scope.grid.data = [];
                }
            });
    };

    $scope.examWatchCollection();

You are over complicating the problem. 你的问题太复杂了。 Simply, don't create a function at all. 简单来说,根本不要创建一个功能。

$scope.$watchCollection('[config.examStatusId, config.examTypeId]',
    function(newValue, oldValue) {
        if (_o.checkWatch(newValue, oldValue)) {
            _u.putConfigs($scope.config);
            $scope.grid.backup = [];
            $scope.grid.data = [];
        }
    });

If you still want a function, you can make that an IIFE like this 如果你仍然需要一个功能,你可以像这样制作一个IIFE

(function() {
    $scope.$watchCollection('[config.examStatusId, config.examTypeId]',
        function(newValue, oldValue) {
            if (_o.checkWatch(newValue, oldValue)) {
                _u.putConfigs($scope.config);
                $scope.grid.backup = [];
                $scope.grid.data = [];
            }
        });
}());

The second version, creates a function object and then invoke it immediately. 第二个版本,创建一个函数对象,然后立即调用它。 Normally, this is used to limit the scope of the variables used by the piece of code, as we don't have block scope in JavaScript. 通常,这用于限制代码段使用的变量的范围,因为我们在JavaScript中没有块范围。 But I strongly believe you should go with the first option. 但我坚信你应该选择第一个选项。

(function () {
    $scope.$watchCollection('[config.examStatusId, config.examTypeId]',
            function (newValue, oldValue) {
                if (_o.checkWatch(newValue, oldValue)) {
                    _u.putConfigs($scope.config);
                    $scope.grid.backup = [];
                    $scope.grid.data = [];
                }
    });
})();

If you never otherwise use it, then you don't need to retain a reference to it. 如果您从未使用它,那么您不需要保留对它的引用。 Just define it anonymously and execute it right away: 只需匿名定义并立即执行:

(function () {
    $scope.$watchCollection('[config.examStatusId, config.examTypeId]',
        function (newValue, oldValue) {
            if (_o.checkWatch(newValue, oldValue)) {
                _u.putConfigs($scope.config);
                $scope.grid.backup = [];
                $scope.grid.data = [];
            }
        });
}());

In JavaScript a function is an object like any other object. 在JavaScript中,函数是一个像任何其他对象一样的对象。 You can execute it from its name or from the definition of the function itself. 您可以从其名称或函数本身的定义中执行它。 Any function, whether referenced by name or defined in-line, will execute if there are parentheses after it. 任何函数,无论是通过名称引用还是内联定义,如果后面都有括号,则会执行。

It might even be simpler to not create a function at all and just, well, execute the code: 根本不创建一个函数甚至更简单,执行代码:

$scope.$watchCollection('[config.examStatusId, config.examTypeId]',
    function (newValue, oldValue) {
        if (_o.checkWatch(newValue, oldValue)) {
            _u.putConfigs($scope.config);
            $scope.grid.backup = [];
            $scope.grid.data = [];
        }
    });

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

相关问题 如何在 javascript 中制作可以直接读取任何文件并用现有文本替换自身的 function? - How to make a function that can directly read any file and replace itself with the text present, in javascript? 如何在JavaScript中从内部调用匿名函数? - How can I call an anonymous function from inside itself in JavaScript? 如何从自身调用导出的函数? - How can I call an exported function from itself? 我怎样才能让这个 javascript 函数调用自己? - How can I get this javascript function to call itself? 如何让 function 调用自身 n 次 - How to make a function call itself n times 我已经声明了一个回调 function 并且我想调用它......但是失败了......我怎么能直接调用它? - I have declared a callback function and I want to call it…but failed…how can I call it directly? 我怎样才能让 withLatestFrom() 与自己一起工作? - How can I make withLatestFrom() work with itself? 如何使用for使其自身移动立方体? - How can I make a cube move by itself with a for? 如何制作一个功能来对 Chrome 进行编程以在我的工作场所将自己设置为“中断”状态 - How can I make a function to program Chrome to set itself the "Break" status at my workplace 为什么我不能直接在 SetTimeout 函数中为 Promise 调用 resolve() - Why can I not call resolve() for Promises in SetTimeout Function directly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM