Javascript: Functions (2)

Source: Internet
Author: User

Parameter arguments

When a function is called, an argments array parameter is obtained. Through its function, you can access the list of all parameters that are passed to it when it is called, including unnecessary parameters that are not defined when the function is defined.

This arguments is not a real array. It has only one Length attribute and can be traversed through index.

Return return

The return statement is used to return data.

If no return value is specified, undefined is returned.

If the function is called in the new method and the returned value is not an object, this (the new object) is returned ).

Exception

var add = function (a, b)
{
if(typeof a != 'number' || typeof b != 'number')
{
throw {name: 'TypeError', message: 'add needs members'};
}
}

var try_it = function ()
{
try {
add("sevem", "ee");
}
catch(e)
{
alert(e.name);
}
}();

Add method to type

Function.prototype.method = function (name, func)
{ if(!this.prototype[name])
{
this.prototype[name]=func;
return this;
}
};
Number.method('ins', function(){
return 2;
});
(3).ins();// value is 2.

Scope

JS has a function scope, so the parameters and variables defined in the function are invisible outside the function. Variables defined at any position in a function are visible anywhere in the function.

You do not need to declare variables as late as possible on JS. The best practice is to declare all variables that may be used in the function at the top of the function body.

JS has only two environments: a global environment and a function environment. That is to say, except for functions, braces in other cases cannot act as fields, and variables in braces can be accessed in external environments.

This in a function has different meanings for different function call methods. For the method call mode, that is, the function is a method of an object, this indicates the object itself. For the function call mode, this is all objects, that is, window. For the constructor call mode, this is the object pointed to by prototype. For apply/call calls, this can be specified by the first parameter.

Closure

The benefit of scope is that internal functions can access parameters and variables (except this and arguments) that define their external functions ).

1. As a reference to a function variable, the function is activated when it returns.

2. A closure is a stack that does not release resources when a function returns.

3. Closure ensures that internal variables are not directly exposed. Used externally.

function a() {
var i = 0;
function b() {
alert(++i);
}
return b;
}

var c = a();
c();

As shown in the code above, the variable C actually points to function B, and variable I is used in function B. When function a's internal function is referenced by a variable outside the function, a closure is created.

Therefore, after function a is executed and returned, GC does not recycle the resources occupied by function. This description is not rigorous, but straightforward.

To better interpret the closure, four concepts are introduced: the execution environment of the function (excution context), the call object, the scope, and the scope chain.

When defining a, the interpreter sets scope chain of A as the environment where A is located when defining a. Because a is a global function, scope chain of A has only window objects. When a is executed, a enters the corresponding excution context. During the creation of the execution environment, a scope attribute is added for A, that is, the scope object of A is the window object in scope chain when a is defined. Then, the excuation context creates a call object. Activity objects are also objects with properties. However, without a prototype, you cannot directly access it through Js. Then add the activity object to the top of the scope chain of. At this time, a's scope chain has two objects. Then add the arguments attribute to the call object to save the parameters passed when calling function. Finally, add the form parameters and internal parameters of all function a to the activity object of function.

This function can access the context when it is created. This is called a closure.

Callback callbacks

Pass the function as a parameter to the function and it will be called when the response is received.

Module

We can use functions and closures to construct modules. A module is a function or object that provides interfaces but hides the state and implementation.

Cascade

The method returns this instead of undefined.
 

Related Article

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.