Moon 01/14 JavaScript--funtion type

Source: Internet
Author: User
Tags define function deprecated

"JavaScript Advanced Programming Chapter 5th page 110"

5.5 function type-------functions are object function names are reference/pointers;

The function is an object. So the function name is actually a pointer to the function object. Does not bind to a function.

Each of these functions is an instance of a function type, with properties and methods like other reference properties,

Ways to create a function:

1. Use function declaration syntax to define function names even if they are required for example

function  sum (sum1,sum2) {     return  sum1+sum2;      }

2. Use the function expression first function:

It is important to note that the final number "is the same as declaring a variable", and there is no function name after a function "because it is not necessary to use a functional expression to define a function, a function name is not required--a reference to a function is called by a variable sum."

var    sum= function (num1,num2) {    retrun num1+ num2;} ;

3. Use function constructors to define functions "deprecated"

The function constructor can accept any number of arguments.  But the last parameter is always treated as a function body, and the previous parameter enumerates the parameters that handle the new function.  Deprecated because this constructor syntax causes parsing of two code (the first is the ECMAScript code that resolves the reimbursement, and the second is parsing the string passed in the constructor. The meaning should be that the function body is parsed two times This sentence is not very understanding the following constructor of the other efficiency of the problem is I search the Internet, so the impact of performance examples

var  sum = new  Function ("Num1", "num2", "return num1+num2");

The function () constructor allows runtime JavaScript code to be created and compiled dynamically. In this way it resembles the global function eval ().

The function () constructor parses the body of the functions each time it executes and creates a new function object. Therefore, it is very inefficient to invoke the function () constructor in a loop or in a frequently executed functions. Instead, the function literal is not recompiled every time it is encountered.

4. The function name is simply a reference

The following example first defines a function named Suma (), which sums and then declares the variable anothersum assigns Suma to Anthersum. Using the function name without parentheses Suma is to access the function pointer (that is, Suma is a reference to the function), rather than calling the function. At this point another and Suma both point to the same function

For example

function  suma (sum1,sum2) {     return  sum1+sum2;   Alert (sum (10,10));              20var  anthersum = Suma;alert (Anothersum (10,23));  suma = Null;alert (anthersum (10,23))       //33

  

55.1 JS is not overloaded, "function name is just a reference to a function object similar to a pointer so there is no overload"

If you declare two functions of the same function name, the function behind the result overrides the previous one.

For example

var   addsomenumber = function (num) {        return num +100;};    var   addsomenumber = function (num) {        return  num + 200;};


var result = Addsomenumber (+); The answer is to call one of the following functions 300

5.52 function declaration "difference" from function expression

How does a 1.JS parser parse function declarations and function expressions?

function declaration: JS parsing is the first to read the function declaration, to ensure that it is available before executing any code; In the following example, before the code executes, the parser has passed a procedure called a function declaration hoisting. Reads and adds a function declaration to the execution environment (the JavaScript engine declares the function at the first pass and places them at the top of the source tree: Therefore, the function code can be declared after calling the function's code)

Function expression: You must wait until the parser is executed sequentially to its line of code before it is really interpreted (such as the second program sum is a variable executed to this sentence to establish a reference to the function) "So the function declaration is equivalent to the method in Java, and the function expression is a new object To make a reference before you can call it in a simple way he is an assignment statement "

Alert (sum (10,10)),           //The executable result is 20function sum (num1,num2) {     return num1+num2;}

Alert (sum (10,10)), the cause of the           error during run time the reference to the  function is not saved in the variable sum until the statement where the function is executed: return "unexpected identifier" (unexpected identifier error) var sum = function (num1,num2) {    return num1+num2;};


Of course you can use both function declarations and function expressions such as var sum = Funciton sum () {} But this syntax causes an error in Safari

5.5. 3 functions as values

1. Pass the function as a value: Because the function name in ECMAScript is itself a variable

<! DOCTYPE html>

It is a very useful technique to return another function from one function.

Moon 01/14 JavaScript--funtion type

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.