Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/styllloz/public_html/qa-include/qa-base.php on line 1175

Warning: session_start(): Cannot start session when headers already sent in /home/styllloz/public_html/qa-include/app/users.php on line 162

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/users.php on line 1267

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/page.php on line 356
Which means construction of a type “(function(d){ ... })(document)” - | Q&A

Which means construction of a type “(function(d){ ... })(document)”

Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
Repeatedly met such a structure:

(function (d) {

// Some code


I understand that this code uses a closure, but it is not clear why the argument of the closed-loop functions share the object Document.

Here is a concrete example — a small Javascript utilitywhich makes it so that in IE you can set the styles for HTML5 elements (like section, header, article and other; the default styles for these elements in IE should be ignored). This script uses the following design:

(function (p, e) {
// Some code
})(this, document);

In this example, in addition to the argument document in addition pass in an argument this, which, as I understand it, is stored in the object Window. Who can tell me why introverted functions to transfer the handling of arguments to Document and Window?
by | 10 views

3 Answers

0 like 0 dislike
1. To limit the scope, i.e. avoid global variables.
2. To reduce the case record to the object.
3. As a special case of the 1st and 2nd to prevent naming conflicts. For example, when using jQuery often write
(function($) { ... })(jQuery);

Thanks to this script, you can use the short record of type $(xxx), even if the identifier $ is defined somewhere else, for example, in MooTools.
P. S. And this, incidentally, is not shorted out.
0 like 0 dislike
Well, you can save a few bytes.
0 like 0 dislike
For me it is the usual encapsulation of functionality.
And at the entrance you can file any variable, for example the document of the iframe and not your functions might be purple

Related questions

0 like 0 dislike
4 answers
0 like 0 dislike
2 answers
asked Apr 11, 2019 by sddvxd
0 like 0 dislike
3 answers
asked Apr 25, 2019 by sorry_i_noob
110,608 questions
257,186 answers
35,181 users