JavaScript series----functions (function) Chapter

Source: Internet
Author: User
Tags abstract definition

1. Functions

The definition of a function in the mass-user is this: a function is an event-driven or reusable block of code that executes when it is invoked.

Admittedly, we don't get anything of value from this abstract definition. Here are some examples of how a function can be defined:

function Add (NUM1, num2) {
  return NUM1 + num2;} var add = function (NUM1, num2) {  return num1 + num2;} This is the more common two kinds of  

The following two species are relatively rare
var add=new Function ("Num1", "num2", "return num1+num2");
var add=function ("Num1", "num2", "return num1+num2");

The above four methods are the correct syntax for creating a function. However, the common is generally the first two types. Because there are some flaws in the latter two compared to the first two types.

  1. The latter two kinds of comparison cumbersome and not intuitive. This can be seen in the above example.
  2. There are some fatal flaws in the latter two. This function is created in such a way that it is not possible to maintain a chain of scopes that are part of a function, and the functions created at any time under the new function are equivalent to those created under the global scope. This can be demonstrated in the following example.
    1. var x = 1; var add = (function () {  var x = +;  return function (num) {    return num + x;  }} ()); Console.log (Add (0));//100    
      var x = 1; var add = (function () {  var x = +;  return new Function (' Num1 ', ' return num1+x ');} ()); Console.log (Add (0));//1   

      In other words, the latter two ways of creating a function cannot form a complete chain of function scopes (which will be discussed later), nor can there be a so-called closure.

  3. The latter two are too inefficient to operate.
    First of all, JS is not efficient for parsing strings, and there are a lot of strings in the (new) function.
    Secondly, JS interpreter, for use function () {}, this form of functions, have some form of optimization. Like the following.
  4. var array = [
    ];
    for (var i = 0; i <; i++) {
    Array[i] = function () {
    return ' undefined ';
    }
    }//the first Kind


    var array = [
    ];
    for (var i = 0; i <; i++) {
    Array[i] = new Function ("return undefined");
    }//the second kind,

there is a big gap between these two approaches in operational efficiency. For, the first one only needs to execute once function () {}, the other 999 times is an assignment, and then a function to execute 1000 times to create and assign a value.

It is because of the three reasons on three sides that the function () {} is more prevalent in this way.

In addition, you may have seen the following, but this form is only a variant.

var New function () {  return 1 + 2;}; Console.log (typeof  add);//objectvar result = add.constructor ();/* The call must be made in this way */console.log (result);//3

In this form, the new function () is created essentially using an anonymous function to create an object. A constructor property of this object just points to its constructor, which is the anonymous function. So it's actually an ugly way of writing.

Here, we are just describing a few ways to define a function. By comparison, we know the first two are more practical, but even so, there is a huge difference between the first and second definitions. In the next section, we'll go on to the difference between the two ways.

JavaScript series----functions (function) Chapter

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.