JavaScript Knowledge points Summary----function definitions

Source: Internet
Author: User

---restore content starts---

function functions:

Functions are actually objects, each of which is an instance of a function type, and all have properties and methods like other reference types, and the function name is actually a pointer to a function that is not bound to a function;

Three ways to define a function:

1. Function declaration

function sum (num1,num2) {    return num1 + num2;  }

2. Function expression, also called function literal

var function (num1,num2) {      return NUM1 + num2}

3. Function construction method, parameters must be quoted

var sum3=New Function (' N1 ', ' N2 ', ' return n1+n2 '); Console.log (sum3 (2,3)); // 5

Three differences: the parser reads the function declaration first and makes it accessible before executing any code, and the function expression must wait until the line of code where the parser is executing is actually interpreted, and there is a semicolon at the end of the function expression, just like declaring other variables. The third method is generally not recommended, because this syntax can result in parsing two of times of code (parsing the regular ECMAScript code for the first time, and the second parsing the string passed into the constructor), thus affecting performance.

<script>alert (SUM (2,3))    functionsum (sum1,sum2) {returnSum1 +sum2; }</script>result:5<script>alert (SUM (2,3))   varsum =function(sum1,sum2) {returnSum1 +sum2; }<script>Result: Error---sum is not afunction<script>varsum =function(sum1,sum2) {returnSum1 +sum2; } alert (SUM (2,3))</script>
Retult:5

Note: Because the function name is just a pointer to a function, a function may have more than one name

---restore content ends---

function functions:

Functions are actually objects, each of which is an instance of a function type, and all have properties and methods like other reference types, and the function name is actually a pointer to a function that is not bound to a function;

Three ways to define a function:

1. Function declaration

function sum (num1,num2) {    return num1 + num2;  }

2. Function expression, also called function literal

var function (num1,num2) {      return NUM1 + num2}

3. Function construction method, parameters must be quoted

var sum3=New Function (' N1 ', ' N2 ', ' return n1+n2 '); Console.log (sum3 (2,3)); // 5

Three differences: the parser reads the function declaration first and makes it accessible before executing any code, and the function expression must wait until the line of code where the parser is executing is actually interpreted, and there is a semicolon at the end of the function expression, just like declaring other variables. The third method is generally not recommended, because this syntax can result in parsing two of times of code (parsing the regular ECMAScript code for the first time, and the second parsing the string passed into the constructor), thus affecting performance.

<script>alert (SUM (2,3))    functionsum (sum1,sum2) {returnSum1 +sum2; }</script>result:5<script>alert (SUM (2,3))   varsum =function(sum1,sum2) {returnSum1 +sum2; }<script>Result: Error---sum is not afunction<script>varsum =function(sum1,sum2) {returnSum1 +sum2; } alert (SUM (2,3))</script>
Retult:5

Note: Because the function name is just a pointer to a function, a function may have more than one name

JavaScript Knowledge points Summary----function definitions

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.