JavaScript Basic Learning-javascript authoritative guide--8.1-8.4 function

Source: Internet
Author: User

Eighth Chapter function

Functions are a very important part of JavaScript, and you can easily read other people's Code by understanding this section, and you'll be familiar with some of the less common code notation.
Control the length of the article according to how easy it is.

Objective:

A function is a JavaScript code that is defined only once and may be executed or invoked any number of times.

If a function is mounted on an object, it is called a method of the object, which is known as the object's property. When the function is invoked through this object, the object is the context of the call , and the function implicitly obtains the context object automatically.

In JavaScript, a function is an object, and the program can manipulate it arbitrarily.

JavaScript functions can be nested within other functions, so that nested functions can access any variable in the scope of the outer function. This means that JavaScript functions form a closure.

8.1 Function definitions

1. Function Features:

 1. 函数名--可选    2. function    3. ()   4.  {}

2. Function Definition Method

    1. Function statements
    2. An expression

      1. The function statement is the normal functions Funname () {}
      Note: When using a function, it must be declared after the function definition or in advance.
      2. The expression is var variable = function () {}
      Note: Use must be a variable already declared.
      The function on the right side of the "=" is an anonymous function that, after creating the function, assigns the function to the left variable.

3. Function naming

Common functions Intrinsic functions, private functions
Like_this () _like_this ()
Likethis ();


Use a short name, or rename it with a symbol.

8.2 Function call 8.2.1 function call

If the function returns because the interpreter reaches the end, the return value is undefined.
If the return statement has no value, it is also returned undefined.

The This keyword cannot normally be used as a function call.

The this in the strict mode function is undefined, which is the global object in non-strict mode

8.2.2 Method Invocation

A method is a function that is stored in an object property.
The most important difference between a method and a function is the invocation context. The function only has global variables and undefined two, and the method determines the context based on the object being called.
This is a keyword that is not a variable or a property name and is not allowed to be assigned a value.
The keyword this does not have a scope limit, and the nested function does not inherit this from the function that called it.
If the nested function is called as a method, this points to the object that called it. If the nested function is called as a function, its this value is a global object (not strict mode), or undefined (in strict mode)

8.2.1 and 8.2.3 are actually the separate invocations of the two methods of defining functions that were previously proposed in 8.1.

8.2.3 Constructor Call

Show return new Object
Constructors without formal parameters can be omitted ()

8.3 arguments and formal parameters of a function 8.3.1 optional formal parameters

Check the parameters and use the new objects if they are not.
When a function is called, the arguments passed in are less than the number of parameters specified when the function is declared, and the remaining parameters are set to the undefined value.

8.3.2 variable-length argument list: Argument object

When the number of arguments passed into the calling function exceeds the number of formal parameters, there is no way to get an unnamed reference directly. Inside the function, the identifier arguments is a reference to an argument object, which is a class array object.
Arguments object, the argument is passed arbitrarily how many parameters, accept the time to use the arguments object as an array of methods obtained.
ECMA5 has removed the argument object.
Arguments can not be used in strict mode parametric name or local variable name, nor can you assign a value.
Non-strict mode is as an identifier

A function can accept an arbitrary number of arguments, called an indeterminate argument function, arguments[] is appropriate for the function that implements this scenario.

Arguments is not a real array, it is an argument object, and modifying its values modifies the arguments that are passed in.

8.3.3 an object property as an argument

Use argument objects + key-value pairs to avoid the location of memory parameters

8.3.4 argument types

Do type checking and handling inside a function

8.4 Functions as values

In the top-level code of the JavaScript, the scope chain consists of a global object.

Does not contain a nested function body, there are two objects on the scope, the first is the object that defines the function arguments and local variables, and the second is the global object.

When you define a function, it actually saves at least one scope chain.

When this function is called, it creates a new object to store its local variables and adds the object to the saved scope chain, creating a new, longer scope chain.

When the function returns, it deletes the bound variable object from the scope chain. If there are no nested functions, and no other references point to the bound object, garbage is recycled.

For nested functions, the inner function is redefined again each time an external function is called. Because each time an external function is called, the scope is different. The

Intrinsic function has a slight difference each time it is defined, and the internal function code is the same every time the external function is called, and the scope chain of the associated code is not the same. The

Nested function does not copy a private member within the scope, nor does it generate a static snapshot of the bound variable.

counter () {       var n=0;        return {          functionreturn n++;},          function() {n=0;}}       ;   }    // each call to counter creates a new scope chain and a new private variable    // -->1   D.count (); // -->1   D.count (); // -->2

闭包实现的原理是将内部引用赋给外部变量,利用外部变量永久保存对内部变量的引用,不会被垃圾回收机制处理掉。

JavaScript Basic Learning-javascript authoritative guide--8.1-8.4 function

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.