What you don't know about JavaScript--about functions

Source: Internet
Author: User

The next thing you don't know about Javascript is a straight line:

    1. Each function is created by default with a prototype property, which contains a constructor property, and a hidden property __proto__ that points to the object. The value of the constructor property is the object of the function. The invocation of a function preceded by new, creates a new object that hides the prototype member of the function (linked by the __proto__ property), and the This of the function is bound to the new object.
    2. The function always returns a value, returns undefined if no return value is specified, returns this if it is called as a constructor, and the return value is not an object, or if the return value is an object, it is meaningless as a constructor!
      [JavaScript] view Plaincopyprint?
      1. function A () {
      2. this.p = ' haha ';
      3. return {p:' Heihei '};
      4. }
      5. var a = new A ();
      alert (A.P);//Display ' Heihei ', as with var a = a ();
    3. Function A directly calls a function b,b the this binding to the global object instead of its external function A, which is an error in the JS design. We have to solve this problem in other ways, such as using a variable (usually that) in a to hold a reference to the this scope of a.
    4. The JS function has a length property that represents the number of formal parameters specified when the function is defined.
    5. The Arguments property of a function contains all arguments passed in when the function is called, regardless of whether the parameters are defined in the declaration of the function, arguments is not an array, just an "array-like" object (running arguments instanceof array in a function; returns false). It can be converted to a JS array by Array.prototype.slice.apply (arguments).
    6. To add a method to the prototype of a JavaScript function, all the (constructor) functions are available! For example, you can add a method to a prototype of the constructor function of a JS function, and all functions, including the constructor such as object, number, inherit the method, which is very powerful:
      [JavaScript]View Plaincopyprint?
        1. Function.prototype.method = function (name, func) {
        2. This.prototype[name] = func;
        3. return this ;
        4. };
      In this way, call the Object.Method method, you can add a new method for all JS objects (including the function object), call the Number.method method, you can add a new method for all numeric types, the following is an example. Note Objects of type object, number, and so on do not inherit method methods at this time. If you want to achieve this, you can run a statement like this:[JavaScript]View Plaincopyprint?
        1. Object.Method (' method ', Object.Method);
    7. We can add a new method to the numeric type by modifying the prototype of the numeric type, here we use the method mentioned in the previous article to add a negative method to the prototype of number:

      [JavaScript]View Plaincopyprint?
        1. Number.method (negative,function () {
        2. return 0– this;
        3. })
      The method is called a little bit around. Inin JavaScript syntax, a number is followed directly by a dot, and then the syntax of the method call is incorrect; thatis, 3.negative () is wrong to write. To invoke a method of a numeric type, you need to add n spaces (n>=1) after the number,or use parentheses to enclose the number, force it into an expression, and then call the method, or simply define a numeric variable, or you can call the method directly. In other words, the following wording is correct:

      [JavaScript]View Plaincopyprint?
        1. (3). negative ();
        2. 3. negative ();
        3. var n = 3; N.negative ();
        4. 3[' negative '] ();
    8. When a function is defined using a function expression method, the function name behind the functions can be used to invoke itself recursively, and the name will not be overwritten! Let's look at the following example,
      [JavaScript]View Plaincopyprint?
        1. function A (n) {
        2. if (n>1)
        3. return A (n-1) +1;
        4. Else
        5. return 1;
        6. };
      The code above defines a function A, and its internal recursive calls itself; now we use a new reference AA to point to function A, and then change the original A to an integer 1, and then call the function AA, as shown in the following code:
      [JavaScript]View Plaincopyprint?
        1. var aa = A;
        2. A = 1;
        3. AA (3);
      the console error: Typeerror:property ' A ' of Object [object Window] is not a function; it is clear that the original recursive function has been destroyed. On this issue, we can use Arguments.callee.caller to replace a in the interior of function A, or to define a function with a named function expression (Named function Expressions) :[JavaScript]View Plaincopyprint?
        1. var B = function A (n) {
        2. if (n>1)
        3. return A (n-1) +1;
        4. Else
        5. return 1;
        6. };
        7. var bb = b;
        8. A = 3;
        9. BB (3);
      At this point, the BB function will correctly return the results we want.
    9. A named function expression is not a true function declaration, and the function name is useful only within the scope of the function . The following code is a good illustration of what it means:

      [JavaScript] view Plaincopyprint?

      1. var B = function A () {
      2. return typeof A;
      3. };
      4. typeof A; //"undefined"
      5. b (); //"function"

    10. To improve the encapsulation of JavaScript functions, we can define a functional constructor, here is an example:
      [JavaScript]View Plaincopyprint?
      1. var funccons = Function (spec) {
      2. var that = {};
      3. That.getname = function () {
      4. return spec.name;
      5. };
      6. That.says = function () {
      7. Return Spec.saying | |  ";
      8. };
      9. return that;
      10. };
      11. var MyFunc = funccons ({name:' Neareast '});
      In this way, we can define some private variables (such as dictionary tables) and functions in the constructor without exposing them all to the outer surface.

What you don't know about JavaScript--about functions

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.