JavaScript oop in-depth learning Note (ii)--javascript functions

Source: Internet
Author: User

First, overview:

Functions are the basis for modular programming, and JavaScript's heavy functions 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 function as a parameter.

Second, recognize the functions in JavaScript

JavaScript can define a function with the function keyword and specify a function name for each function, which is called by the name.

When JavaScript interprets execution, the function is maintained as an object, which is the function object.

A function object is fundamentally different from other user-defined objects, which are called internal objects. For example, a Date object (date), an array object, or a String object (string) belong to an internal object. The constructors for these built-in objects are defined by the javascripy itself, which returns an object by executing a statement such as Array (), within which 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 functions, just as the array object corresponds to 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.

    <script>        function $myfunction ($a, $b) {      // traditional notation            var $res = $a + $b;           document.write ($res);             return $res;        }     </script>

  We use another notation:

<script>    varnew Function ("$a", "$b", "var $res = $a + $b;d ocument.write ($res); return $ res; " )</script><body>    

  Actually the result is the same.

  

var 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.

Note that P1 to PN is a list of parameter names, and P1 not only represents a parameter, it can also be a comma-separated list of parameters.

JavaScript introduces a type of 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 function essence is an internal object, which is determined by the JavaScript interpreter, and functions created by the code above can be called using the function name in the program. Immediately after the function declaration, the parentheses indicate that the function call is made as soon as the creation is complete.

Iii. 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 new operator that returns an object. The function object, however, differs from other objects in that it returns the string ' function ' when using typeof to get the type of a function object, and returns the string ' object ' when typeof an array object or other object.

  Although the function itself is an object, it is different from a normal object, and it should be a colleague as well as an object constructor, that is, a new function can be used to return an object.

Functions are the basis for all function objects, and object is the basis for all objects, including function objects. In JavaScript, any object is an instance of object. Therefore, you can modify the object type to have all objects have a write-common property and method, and modifying the object type is done through prototype.

Iv. passing a function object as a parameter

Each function is represented as a special object that can be conveniently assigned to a variable and then called by the variable name. As a variable, it can be passed to another function in the form of an argument.

function func1 (thefunc) {  thefunc ();     } function Func2 () {  alert ("good");} Func1 (FUNC2);

JavaScript oop in-depth learning Note (ii)--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.