JavaScript function functions in depth summarize __ block chain

Source: Internet
Author: User

The various functions of function in JavaScript are sorted out, the sense function is a big object, all kinds of knowledge points can be involved, not only function this itself native reference type of various usages, but also include execution environment, scope, closure, context, A deep understanding of the knowledge points such as private variables.

Return in a function

The return statement can be without any returned value, in which case returns; Or when a return statement is not included in a function, the function returns the Undefiend value after it stops executing. This usage is generally done in cases where the function needs to be stopped prematurely without having to return a value.
return FALSE to block the default event for an element.
Return returns the return value of the function in which it is located
function N () {
(function () {
return 5;
})();
}
n ();//undefined
Executing the return statement immediately in an anonymous function is actually returned to the anonymous function where it resides.

function N () {
var num= (function () {
return 5;
})();
Console.log (num);
}
function type

The function is actually an object, and each function is actually an instance of the type of the functions. and has attributes and methods as well as other reference types. A function name is actually a pointer to a function object in the memory heap.

How to define a function

function declaration
function sum (num1,num2) {
return num1+num2;
}
function expression
var sum=function (num1,num2) {
return num1+num2;
};
Defines a variable sum and initializes it to a function, noting that there is no function name after the functions keyword, because it is not necessary to use a function expression to define a function, but to refer to a function by using the variable sum. Also note that there is a semicolon at the end of the function, just like declaring other variables.

The new constructor, although this usage is also a function expression, is not recommended. Because this syntax causes parsing of two-time code (the first time parsing the regular ECMAScript code, the second is parsing the string in the incoming constructor), affecting performance.
Using the function constructor, a constructor can accept any number of arguments, but the last argument is always considered a function body, and the preceding argument enumerates the parameters of the new function.
var sum=new Function (' num1 ', ' num2 ', ' return num1+num2; ');
sum;//
function Anonymous (num1,num2
/**/) {
return num1+num2;
}
When you use a function name without parentheses, you access the function pointer, not the function.

Understanding Parameters

All parameters in the ECMAScript are passed values (even if the reference is also the address value passed, not the reference pass parameter (whether the JavaScript pass parameter is passed by value or by reference). The ECMAScript function does not mind passing in the number of arguments, nor does it care what data type the incoming parameter is. This is because the parameters in the ECMAScript are represented internally by an array. This array is always received by the function, and does not care what parameters are included in the array. In a function body, you can access this array by arguments the object. To get each parameter passed to the function.

function func () {
Console.log (Object.prototype.toString.call (arguments));
}

Func ();//[Object Arguments]
With respect to the behavior of arguments, its value is always synchronized with the value of the corresponding named parameter. Because the values in the arguments object are automatically reflected to the corresponding named arguments. So modify Arguments[1], also modify the num2. This is not to say that reading the two values accesses the same memory space, their memory space is independent, but their values are synchronized (WHY). If JavaScript can access memory directly, verify it.
However, if only one argument is passed in, the value set by arguments[1] is not reflected in the named argument, because the length of the arguments object is determined by the number of incoming arguments, not by the number of named arguments that define the function, and the named arguments that do not pass the value are automatically assigned Undefiend value, which is the same as defining a variable but not initializing it.
function Doadd (num1,num2) {
Console.log (arguments.length);
Console.log (NUM2)
arguments[1]=10;
Console.log (NUM2);
}
Doadd (5,0);//2 0 10

Doadd (5);//1 undefiend undefined
No overload

ECMAScript functions cannot be overloaded in the traditional sense, while in other languages (Java), you can write two definitions for a function, as long as the two signatures (the type and number of parameters received) are different.

Reasons for overloading cannot be implemented:

The ECMAScript function has no signature because its arguments are represented by an array that contains 0 or more values. Without a function signature, the real overload is impossible. When you define a function in ECMAScript that has the same name as two, the name belongs only to the later defined function. How to implement an overload similar to Java, you can actually make a different response by judging the type and number of arguments passed in to the function.
function Reload () {
if (arguments.length==0) {
Console.log (' no reference ');
}else if (arguments.legth==1) {
Console.log (' passed a parameter ');
}
}
Deep understanding: Imagining a function name as a pointer also helps to understand why there is no concept of a function overload in ECMAScript.
function Add () {
return 100;
}
function Add (num) {
return num+200;
}

It's actually no different from the following code.
function Add () {
return 100;
}
Add=function (num) {
return num+200;
}
function declarations and function expressions

In fact, the parser does not treat function declarations and function expressions equally when loading data into the execution environment.

A probe into the operation mechanism of JavaScript and understand that for interpreted languages, the compilation steps are:

Lexical analysis (converts a character stream to a token flow, which is a one-to-one translation of a hard-read stream of tokens)
Parsing (the so-called variable elevation operation, in fact, I think is to keep these ascending variables in the syntax tree.) To construct a syntax tree, you will report a syntax error if you fail to construct it and end parsing of the entire block of code.
Then there may be semantic checking, code optimization, and so on. After getting the syntax tree, I began to explain the execution. The explanatory language is not compiled into binary code but is executed from the syntax tree.
The parser reads the function declaration first and makes it available before any code is executed. As for function expressions, you must wait until the execution phase is actually assigned. What does that mean. Although both are subject to variable elevation, the construction of the active object is added to the execution environment from the syntax tree declaration to the actual execution, but one is the function elevation and the other is the variable elevation.

function declaration
Console.log (func);//function func () {}
function func () {

}

function expression
Console.log (func1);//undefined
var func1=function () {};
Console.log (func1);//function () {}
function as a value

Because the function name in ECMAScript itself is a variable, the function can also be used as a value. You can pass a function to another function just as you would pass a parameter, and you can return a function as the result of another function.

function Callsomefunction (somefunction,someargument) {
Return someFunction (someargument);
}

function concated (str) {
Return "Hi" +STR;
}

Callsomefunction (concated, ' xx ');//' Hi xx '
Returns the application of another function from one function: Suppose you have an array of objects that you want to sort based on an object property, but the comparison function passed to the sort () method receives two parameters, the value to compare. We need a way to indicate which attribute to sort by. We can define a function that receives a property name and then creates a comparison function based on the property name. By default, the sort function invokes the toString () method of each object to determine their order.

function Createcompare (property) {
return function (OBJ1,OBJ2) {
var Value1=obj1[property],
Value2=obj2[property];
if (value1

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.