Getting Started with Ajax deep understanding of functions in JavaScript

Source: Internet
Author: User
Tags definition function definition

function is the basis of modular programming, writing complex AJAX applications, you must have a more in-depth understanding of the function. Functions in JavaScript are different from 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 to pass the function as a parameter. Before continuing, take a look at the use syntax of the function:

function Func1 (...) {...}
var func2=function (...) {...};
var func3=function func4 (...) {...};
var func5=new Function ();

These are the correct syntax for declaring a function. They are very different from functions that are common in other languages, or functions that are previously described. So why do you write that in JavaScript? What is the syntax it follows? These are described below.

Understanding function Objects (Functions object)

You can define a function by using the function keyword, and specify a function name for each function, which is invoked by the name of the functions. When JavaScript interprets execution, the function is maintained as an object, which is the function object to introduce.

function objects are essentially different from other users ' defined objects, which are called internal objects, such as date objects (dates), array objects (array), string objects (strings), which are internal objects. The constructors for these built-in objects are defined by JavaScript itself: returning an object by executing a statement such as the new Array (), where JavaScript has a mechanism to initialize the returned object rather than the user specifying how the object is constructed.

In JavaScript, the type of the function object corresponds to functions, just as the type of the array object is the array, and the Date object corresponds to the type date, and you can create a function object by using the 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 arrays of objects. First look at the array object: The following two lines of code are to create an array object myarray:

var myarray=[];
Equivalent to
var myarray=new Array ();
Again, the following two pieces of code create a function MyFunction:
function MyFunction (a,b) {
return a+b;
}
Equivalent to
var myfunction=new Function ("A", "B", "Return A+b");

By comparing and constructing the Array object statement, you can clearly see the nature of the function object, the function declaration described earlier is the first way of the above code, but inside the interpreter, when this syntax is encountered, a function object is automatically constructed and stored and run as an internal object. As you can see here, a Function object name (function variable) and a normal variable name have the same specification, can be referenced by variable name to this variable, but the function variable name can be followed by parentheses and argument list for function calls.

Creating a function in the form of the new function () is uncommon because a function body usually has multiple statements that, if passed as a string, are not readable by the code. Here's how to use the syntax:

var funcname=new Function (p1,p2,..., pn,body);

The type of the parameter is a string, p1 to the list of parameter names for the function created by the PN, and the body represents the function statement of the function being created, FuncName is the name of the function being created. You can create an empty function without specifying any parameters, and do not specify funcname to create a nameless function, of course, that function has no meaning.

Note that P1 to PN is a list of parameter names, that is, P1 not only represents an argument, it can also be a comma-separated list of arguments, such as the following definition is equivalent:

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")

The syntax for JavaScript to introduce a function type and provide a new function () is because the function object adds properties and methods that must rely on the type of function.
The essence of a function is an internal object that determines how it is run by the JavaScript interpreter. Functions created from the above code can be invoked in a program using the name of the function. The function definition issues listed at the beginning of this section are also explained. Note You can add parentheses directly after a function declaration to indicate that a function call is made immediately after the creation completes, for example:

var i=function (a,b) {
return a+b;
} (1,2);
alert (i);

This code will show that the value of variable I equals 3. I is the value that is returned, not the function created, because the parentheses "(") have a higher priority than the equals sign = ". Such code may not be commonly used, but this is a good solution when users want to design a module in a long piece of code or want to avoid naming conflicts.

It is worth noting that although the following two methods of creating functions are equivalent:

function FuncName () {
function body
}
Equivalent to
var funcname=function () {
function body
}

But one way to do this is to create a well-known function, and then create a nameless function, and just let a variable point to the Nameless function. There is only one difference in use: For a well-known function, it can be defined after the call, and for the nameless function it must be defined before the call. For example:

This statement will produce Func undefined errors, and:

is executed correctly, the following statement can also be executed correctly:

This shows that although JavaScript is an interpreted language, it will check the entire code for the existence of a function definition in the function call, which is only valid through the function funcName () definition, not an anonymous function.

Relationship of function objects and other internal objects

In addition to the function object, there are many internal objects, such as Object, Array, Date, RegExp, Math, and Error. These names actually represent a type, and you can return an object by using the new operator. However, unlike other objects, the function object will still return the string "function" when it typeof the type of a function object, and it will return the string "Object" when typeof an array object or other object. The following code example typeof different types of cases:

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.