Function Type Details in javascript, javascriptfunction

Source: Internet
Author: User

Function Type Details in javascript, javascriptfunction

Function Type

The function type is undoubtedly a very important component in js.

1. This is first an object, that is, it is a reference type. Statement: when an object is said to be an object, is there a kind of base class that is an object illusion? No,

It is independent of the object. When you type of function, the returned funciton is not an object.

2. Each Function is an instance of a Function object. It has attributes and methods like other referenced objects. Because it is an object, function names are pointers to function objects.

Syntax support for function declaration:

<Script> // method 1 function fun (num1, num2) {return num1 + num2;} // method 2 var fun = function (num1, num2) {return num1 + num2 ;}; // method 3 var fun = new Function ("n1", "n2", "return n1 + n2"); </script>

Explanation: each of the three methods can complete the declaration of a function, but they are different.

Method 1 is interpreted as a function declaration, method 2, and method 3 as a function expression. (Method 3 is not recommended because the code is parsed twice. First, the general ECMAScript Code is interpreted, and the passed parameters are interpreted.

The number of parameters can be N, but the last parameter is regarded as the function subject)

The main difference lies in the difference between the js Parser's function declaration and function expression parsing. The parser first reads the function declaration, and the js engine automatically puts the function declaration at the top of the execution environment during execution.

Function expressions are different. When a function expression is executed, it is actually interpreted and executed. It is important to understand this point!

View code

<script>  console.log(typeof fun); //"function"  console.log(typeof fun2); //"undefined"  console.log(typeof fun3); //"undefined"  function fun(n1,n2){   return n1+n2;  }  var fun2=function(n1,n2){   return n1+n2;  }  var fun3=new Function("n1","n2","return n1+n2;"); </script>

3. Why is the function not overloaded?

This issue should be considered from the language features of js. As mentioned in Article 2nd, the function name is just a pointer to the function object. It is clear to understand the concept of pointer.

Sample Code:

<Script> function fun (n1) {return n1 + 100;} function fun (n1) {return n1 + 200;} console. log (fun (1); // 201 // The above statement should be var fun = function (n1) {return n1 + 100 after parsing;} fun = function (n1) {return n1 + 200;} // reference of fun is overwritten by console. log (fun (1); // 201 </script>

Writing this today is a basic concept, and I hope it will be helpful for this blind spot! I hope you can leave a message to correct your suggestion.

The above is all the content of this article. I hope you will like it.

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.