Concepts of functions

Source: Internet
Author: User

Concepts of functions

Overview

Function Name Improvement

// Error mode f (); var f = function () {}; // TypeError: undefined is not a function // The correct mode f (); function f () {}// the above Code is equivalent to var f; f (); f = function () {}; <strong> </strong>


Function Scope

Internal Variable upgrade

Like global scopes, function scopes also generate "variable escalation. The variables declared by the var command are promoted to the header of the function body no matter where they are.

(function foo(x) {    if (x > 100) {         var tmp = x - 100;    }    alert(tmp);})(101);// 1


Parameters

Default Value

The following method can be used to set the default value for function parameters.

function f(a){    a = a || 1;    return a;}f('') // 1f(0) // 1

| Of the above Code indicates "or operation", that is, if a has a value, a is returned; otherwise, a preset default value is returned (in the above example, 1 ). This write method performs a Boolean operation on a. a is returned only when the value is true. However, apart from undefined, the Boolean values of 0, null, and null are also false. That is to say, in the preceding function, a cannot be set to 0 or a Null String. Otherwise, the default value is returned when a parameter is explicitly set. To avoid this problem, we can use the following more accurate statement.

function f(a){    (a !== undefined && a != null)?(a = a):(a = 1);    return a;}f('') // ""f(0) // 0



Arguments object

Relationship with Arrays

To make the arguments object use the array method, the real solution is to convert arguments into a real array. The following are two common conversion methods: slice Method and filling in the new array one by one.

var args = Array.prototype.slice.call(arguments);// or var args = [];for(var i = 0; i < arguments.length; i++) {      args.push(arguments[i]);}


Other knowledge points

Closure

Closure is a function defined in the function body. More theoretically, closures are a combination of functions and the scope object in which they are generated.

function f() {    var c = function (){}; }

In the code above, c is a function defined in function f, that is, a closure. The closure feature is that internal variables of a function can be read outside the function.

function f() {    var v = 1;    var c = function (){        return v;    };    return c;}var o = f();o();// 1
The code above indicates that, originally outside function f, we cannot read the internal variable v. However, with the help of closure c, you can read this variable.

The closure can not only read the internal variables of the function, but also make the internal variables remember the operation results of the last call.

function fn(a) {        return function () {             return a++;        }}var o = fn(1);o() // 1o() // 2o() // 3
The code above indicates that the start variable in the function is calculated based on the value of the previous call every time.


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.