最近我遇到了一段像这样的代码:
var noop = function(){};
options.ondragover = options.ondragover || noop;
options.ondragleave = options.ondragleave || noop;
options.ondrop = options.ondrop || noop;
options.onfilesdone = options.onfilesdone || noop;
答案 0 :(得分:2)
这是无操作功能
在以下链接中详细讨论
https://disqus.com/home/discussion/chrislaughlin/noop_no_operation_function/oldest/
答案 1 :(得分:0)
您发布的代码声明了一个名为noop(No Operation)的空函数,作为在某些条件适用时执行的替代方法。 例如代码:
ColPattern
检查FROM OPENXML (@hDoc, '/Transfer/Products/Product',2)
WITH (
Identifier varchar(80),
ProductItemCode varchar(10),
SerialNumber varchar(48),
NetWeight decimal(13,2),
GrossWeight decimal(13,2),
PackDate datetime 'Attribute[@Name = "PackDate"]',
PlantID int 'Attribute[@Name = "PlantID"]',
SlgrDate datetime 'Attribute[@Name = "SlgrDate"]'
)
是否存在,如果没有,则将空函数分配给变量。
答案 2 :(得分:-3)
它只是一个自我执行的函数,无论你声明什么,你都可以执行相同的操作。
它限制范围并将其设为私有并隐藏全局对象中的变量。
// Somewhere it is defined as global..
var x = 7;
// Your piece of code
var x = "roman" // Here, you override the value of x defined earlier.
alert(x); // "roman"
But when you use a closure which you have mentioned:
var x = 7;
// it doesn't affect/change the value of global x
(function (){ var x = "roman";})();
alert(x); // 7