Recognize JS function objects (Functions object)

Source: Internet
Author: User

Recognizing a Function object can define a function with the function keyword, and for each function it can give it a function name, which is called by its name. These are the impressions that the code gives to the user, and when JavaScript interprets execution, each function is actually maintained as an object, which is the function object that this section will cover. 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). In other words, the constructors for these built-in objects are defined by the JavaScript itself: by executing a statement such as the new Array (), an object is returned, and JavaScript has a set of mechanisms 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, the creation of function objects and the creation of group objects are compared. Look at the Array object first: The following two lines of code are the same, all creating an Array object myarray:varmyarray=[];//equivalent tovarmyarray=NewArray (); Again, the following two-segment code is equivalent to creating a function MyFunction:functionMyFunction (A, b) {returnA +b;}//equivalent tovarmyfunction=NewFunction ("A", "B", "Return A+b")and now the above code is still a bit difficult to understand, but through and constructs the Array object statement comparison, can clearly see the function's object essence, the function declaration described above is the first way of the above code, and inside the interpreter, when encountered this syntax, will automatically construct a function object, Stores and runs functions as an internal object. 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. Perhaps no one can create a function in the form of new function (), because a body of functions usually has more than one statement, and if you pass them as parameters as a string, the readability of the code is very poor. Here's how to use the syntax:varFuncname=Newfunction (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 created function. You can create an empty function without specifying any arguments, and do not specify funcname to create a nameless function, and of course that function is useless. Note that the preceding P1 to PN is a list of parameter names, which means that P1 not only represents a parameter, it can also be a comma-opened argument list, for example, the following definition is equivalent:NewFunction ("A", "B", "C", "Return A+b+c"))NewFunction ("A, B, C", "Return A+b+c"))NewFunction ("A, B", "C", "Return A+b+c")It is not meaningless for JavaScript to introduce a function type and provide syntax such as new function () to create functions, which can be seen later as an object, which itself can have some methods and properties. Adding properties and methods to function objects must be aided by the type of functions. Now that you've realized the nature of the function, it's actually an internal object, and the JavaScript interpreter determines how it works. Functions created by the code above can be called using the function name in the program. The function definition issues that are listed at the beginning of this section are also explained: they are the correct syntax for creating function objects. Note that adding parentheses directly after the function declaration indicates that a function call is made immediately after the creation is complete, for example:varI=function(A, b) {returnA +b;} (Thealert (i); This code shows the value of the variable I equals 3. I is the value that is returned, not the function created, because the parentheses "(" Equals "="There is a higher priority. 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:functionFuncName () {//function Body}//equivalent tovarFuncname=function(){//function BodyBut 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:<script language= "JavaScript" type= "Text/javascript" ><!--func ();varFunc=function() {alert (1)}// -</script>This statement generates a Func undefined error, and:<script language= "JavaScript" type= "Text/javascript" ><!--func ();functionfunc () {alert (1)}// -</script>can be executed correctly, even the following statements are executed correctly:<script language= "JavaScript" type= "Text/javascript" ><!--func ();varSomefunc=functionfunc () {alert (1)}// -</script>This shows that although JavaScript is an interpreted language, it will check the entire code for a function definition when making a function call, which is only valid if defined by function FuncName (), not an anonymous function. 

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.