Understanding JavaScript functions

Source: Internet
Author: User

Functions are the basis for modular programming, and for writing complex AJAX applications, you must have a deeper understanding of the functions.

The functions in JavaScript are different from those of other languages, and each function is maintained and run as an object. By the nature of the function object, it is convenient to assign a function to a variable or pass the function as a parameter. Before continuing, take a look at the usage syntax of the function: The following is a reference fragment:
function Func1 (...) {...}
var func2=function (...) {...};
var func3=function func4 (...) {...};
var func5=new Function (); These are the correct syntax for declaring a function. They differ greatly from functions that are common in other languages or the way they are defined earlier. So why would you write that in JavaScript? What is the syntax it follows?  These are described in the following sections. The cognitive function object can define a function with the function keyword, and specify a function name for each function, which is called by its name.  When JavaScript interprets execution, the function is maintained as an object, which is the function object to be introduced. A function object is fundamentally different from other user-defined objects, which are called internal objects, such as date objects (dates), array objects (arrays), and string objects (strings).  The constructors for these built-in objects are defined by JavaScript itself: by executing a statement such as the new Array (), an object is returned, and there is a set of mechanisms within JavaScript to initialize the returned object, rather than the user specifying how the object is constructed. In JavaScript, the function object corresponds to the type of functions, just as the array object corresponds to the type is an array, the Date object corresponds to the same type as date, you can create a function object by using new function (). You can also create an object by using the function keyword. For ease of understanding, we compare the creation of function objects and the creation of array objects. Look at the Array object first: The following two lines of code create an array object myarray: The following is a reference fragment:
var myarray=[];
Equivalent to
var myarray=new Array ();
Similarly, the following two-segment code also creates a function MyFunction:
function MyFunction (A, b) {
return a+b;
}
Equivalent to
var myfunction=new Function ("A", "B", "Return A+b"); Through the comparison with the construction of the array object statement, you can clearly see the function object essence, the function declaration described above is the first way of the above code, and inside the interpreter, when encountering this syntax, it will automatically construct a function object, which functions as an internal object to store and run.  As can be seen here, a function object name (function variable) and a normal variable name have the same specification, you can refer to this variable by the variable name, but after the function variable name can follow the parentheses and the argument list to make function calls. Creating a function in the form of new function () is uncommon because a function body usually has more than one statement, and if you pass them as arguments as a string, the readability of the code is poor. Here's how to use the syntax: The following is a reference fragment:
var funcname=new Function (p1,p2,..., pn,body);
The type of the parameter is a string, p1 to PN represents the list of parameter names of the created function, body represents the function body statement of the created function, and funcname is the name of the function being created.  You can create an empty function without specifying any arguments, and do not specify funcname to create a nameless function, which of course does not have any meaning. It should be noted that P1 to PN is a list of parameter names, that is, P1 can not only represent a parameter, it can also be a comma-separated list of parameters, for example, the following definition is equivalent: The following is the reference fragment:
New Function ("A", "B", "C", "Return A+b+c")
New Function ("A, B, C", "Return A+b+c")
New Function ("A, B", "C", "Return A+b+c")
JavaScript introduces a function type and provides a syntax such as the new function () because the function object must use the type of functions to add properties and methods.

The essence of a function is an internal object that the JavaScript interpreter determines how it runs. Functions created by the code above can be called using the function name in the program. The function definition issues listed at the beginning of this section are also explained. Note that you can directly enclose the function declaration with parentheses to indicate that a function call is made immediately after creation, for example: The following is a reference fragment:
var i=function (A, b) {
return a+b;
} (for each);
alert (i); This code will show that the value of the variable I equals 3. I is the value returned, not the created function, because the parentheses "(" is higher precedence than the equals sign "=".  Such code may not be common, but it is a good solution when users want to design a module in a long code snippet or avoid naming conflicts. It is important to note that although the following two methods of creating functions are equivalent: The following is a reference fragment:
function FuncName () {
function body
}
Equivalent to
var funcname=function () {
function body
}
But the first way to create a well-known function is to create a nameless function, just to have a variable point to the Nameless function. There is only a small difference in usage: for a well-known function, it can be defined after the call, and for the nameless function, it must have been defined before the call. For example: The following is a reference fragment:
<script language= "JavaScript" type= "Text/javascript" >
!--
Func ();
var func=function () {
Alert (1)
}
-->
</script>
This statement produces a Func undefined error, while: The following is a reference fragment:
<script language= "JavaScript" type= "Text/javascript" >
!--
Func ();
function func () {
Alert (1)
}
-->
</script>
is executed correctly, the following statement is also executed correctly: The following is a reference fragment:
<script language= "JavaScript" type= "Text/javascript" >
!--
Func ();
var somefunc=function func () {
Alert (1)
}
-->
</script>
This shows that although JavaScript is an interpreted language, it will check the entire code for a function definition when it is called, only if the function name is defined through function funcName (), and cannot be an anonymous function.

Understanding JavaScript functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.