Recently I came across a piece of code which was like this:
var noop = function(){};
options.ondragover = options.ondragover || noop;
options.ondragleave = options.ondragleave || noop;
options.ondrop = options.ondrop || noop;
options.onfilesdone = options.onfilesdone || noop;
This is a No Operation function
Discussed in detail in following link
https://disqus.com/home/discussion/chrislaughlin/noop_no_operation_function/oldest/
The code you posted declared an empty function with the name noop (No Operation) as an alternative to execute when certain conditions apply. For example the code:
options.ondragover = options.ondragover || noop;
checks if options.ondragover
exists and if not assigns the empty function to the variable.
It is simply a self executed function in which whatever you declare, you can execute the same.
It restrict the scope and make it private and hide the variables from global objects.
// 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.