First look at a piece of PHP code:
<?php $m = new message();$m->index();class message { //$info = "hello"; public function index(){ $info = "hello"; echo $info; }}?>
This can be executed, but if you use the annotation method, an error will be reported. At least nothing is output.
What's amazing is:
<script type = "text/javascript"> var info = "hello"; function index(){ alert(info); }</script>
This code can indeed be executed. NoIndexMethod to any parameter. But can outputInfo. The index () method here is a closure.
What about jquery?
See the following method:
(function($){ $("#name")click(function(){ alert("hello,are you ok?"); });})(jQuery);
The $ symbol here is only a parameter, but jquery is indeed a global variable, so it will be automatically executed without calling this function.
Just like
function test($){$("#name").click(function(){});}test(jQuery);
It should be known that in the JS file, it is written directly in:
<script type = "text/javascript"></script>
The code in is executed when the document is loaded.
Therefore, this function is automatically executed without calling it.
First, let's take a look at the concept of anonymous functions:
To use a function, we must first declare its existence. The most common method is to use a function statement to define a function, such:
function abc(){ //....}
Of course, your function can also contain parameters or even return values.
function(x,y){ alert(x+y);}
However, no matter how you define your function, the JS interpreter translates it into a function object. For example, if you define the function number of one of the above examples, enter the following code:
alert(typeof abc);// "function"
Function object
Function objects are inherent objects in Javascript. All functions are actually function objects. We will leave this discussion to the next special topic section. Let's first look at whether the function object can directly use the constructor to create a new function? The answer is yes. For example:
var abc = new Function("x","y","return x*y;");alert(abc(2,3)); // "6"
I believe you have some knowledge about how to declare a function. So what is an anonymous function?
Declare anonymous Functions
As the name implies, an anonymous function is a function without a real name. For example, we can remove the name of the function in the above example and determine whether it is a function:
alert(typeof function(){});// "function"alert(typeof function(x,y){return x+y;});// "function"alert(typeof new Function("x","y","return x*y;"))// "function"
We can easily see that they are all function objects. In other words, they are all functions, but they all have a feature-No Name. So we call them "anonymous functions ". However, we cannot find them because they do not have a "name. This raises the question of how to call an anonymous function.
Call of anonymous Functions
To call a function, we must have a method to locate it and reference it. Therefore, we need to find a name for it. For example:
var abc=function(x,y){ return x+y;}alert(abc(2,3)); // "5"
The above operation is actually equivalent to defining a function in another way. This usage is frequently encountered. For example, when we set a DOM element event processing function, we usually do not set their names, but assign the corresponding event to reference an anonymous function.
There is also another way to call an anonymous function, that is, the jquery snippet we see -- use () to enclose an anonymous function, and then add a pair of parentheses (including the parameter list ). Let's take a look at the following example:
alert((function(x,y){return x+y;})(2,3));// "5"alert((new Function("x","y","return x*y;"))(2,3));// "6"
Do you know the role of parentheses? Parentheses can combine our expressions into blocks, and each block, that is, each pair of parentheses, has a return value. The returned value is actually the return value of the expression in parentheses. Therefore, when we enclose anonymous functions with a pair of parentheses, in fact, parentheses return the function object of an anonymous function. Therefore, adding an anonymous function to the parentheses is like a function with a name, and we get its reference position. Therefore, if the parameter list is added after the referenced variable, the call form of the common function is implemented.
I don't know if you can understand the above text. If you still can't understand it, let's try the following code again.
VaR abc = function (x, y) {return x + y ;}; // assign the anonymous function object to the constructor of ABC // ABC, just like the constructor of the anonymous function. That is to say, the implementation of the two functions is the same. Alert (ABC). constructor = (function (x, y) {return X + Y;}). constructor );
PS: constructor refers to the function for creating objects. That is, the function body represented by the function object.
In short, it is understood as the function object returned by the parentheses expression (the anonymous function included in the parentheses), and then the function object can be called as a normal parameter list. (The preceding error is reported. Only function expressions cannot directly call functions. To remove Anonymous function brackets, you must assign values to the expressions. That is, (function () {alert (1)}) () should be equivalent to a = function () {alert (1, cannot even remove a = .)
ClosureWhat is a closure? A closure refers to a code block in a programming language that allows a level-1 function to exist and the free variables defined in the level-1 function cannot be released until the level-1 function is released, these unreleased free variables can also be applied to a level-1 function.How? See a sweat ...... It's okay, so do I (although I understand it, it's just a matter of expression ability ). Let's change to a simpler method: closure is actually a language feature. It refers to a programming language that allows functions to be viewed as objects, then, you can define instance (local) variables in functions like operations in objects. These variables can be saved to the function until the Instance Object of the function is destroyed, other code blocks can get the value of these instance (local) variables in some way and perform application extension. I don't know whether it will be clearer after this explanation. If I still don't understand it, Let's simplify it: closure, actually, it refers to the local variables defined in the function that can be called by the code in the program language. Now let's look at an example:
VaR abc = function (y) {var x = y; // This is the local variable return function () {alert (x ++ ); // here, X of the local variable of the first-level function in the closure feature is called and alert (y --) is operated on it --); // The referenced parameter variable is also a free variable} (5); // initialize ABC (); // "5" "5" ABC (); // "6" "4" ABC (); // "7" "3" alert (x); // error! "X" is not defined!
Can you determine whether the code snippet of jquery is closed?
Let me understand it. Whether the closure feature is applied, you must determine whether the Code contains the most important elements: local variables not destroyed. Obviously, the closure feature cannot be applied without any implemented anonymous functions. But what if an anonymous function is implemented? You have to determine whether the local variables that are not destroyed are used in its implementation. So if you ask what features of JS are applied to the jquery code snippet in the beginning? Then it is just a call of anonymous and anonymous functions. However, it implies the closure feature and can implement the closure application at any time. Because JS is born with this feature!
Article reprinted: http://blog.csdn.net/natineprince/article/details/4759533