[英]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.