JavaScript Learning Notes (a) function

Source: Internet
Author: User
Tags closure

function MyFunc (theobject) {  theobject.make = "Toyota";} var Mycar = {make: "Honda", Model: "Accord", Year:1998},var x, y;x = Mycar.make;     X Gets the value "Honda" MyFunc (mycar); y = Mycar.make;     Y Gets the value "Toyota"                    //(make attribute changed by function)

 

function declaration: 1. Function name 2. parameter list 3. function function

Define a function:

var number = 10;
function Square (number) {
Number = Number*number; return number; }
var x = square (number);
Console.log (number);//10
Console.log (x);//100

Function name Square

Parameter number

function function return number^2

Raw parameters and objects as arguments (the two sets of code above are instances)

1. When the original parameter is used as a function parameter, changing the parameter inside the function does not affect the global or call function

2. If the parameter is an object, an array, or a custom object that changes the object's properties inside the function, this change is visible to the outside of the function.

function expression

/* Function expression for the anonymous function */var square = function (number) {  return number * Number};var x = square (4);/* function expression with function name */var factorial = function FAC (n) {return n<2 1:N*FAC (n-1)};console.log (factorial (3));

function as a parameter

function Map (f,a) {  var result = [],//Create a new array      i;  for (i = 0; I! = a.length; i++)    result[i] = f (a[i]);  return result;}

function f () as a parameter of the map () function

Map (function (x) {return x * x * x}, [0, 1, 2, 5, 10]);

  Returns [0, 1, 8, 125, 1000].

function hoisting is only used for function declarations not for function expressions

Console.log (Square (5));/* */function Square (n) {return n*n}

  

Console.log (square); Square is hoisted with an initial value Undefined.console.log (square (5)); Typeerror:square is not a functionvar square = function (n) {   return n * N;}

function scope

Functions can access their own internally defined variables or functions, and global access to all variables or functions defined in global scope

Child functions can access variables defined inside the parent function or other variables whose parent functions have access.

Scope and function stacks (scope and functions stack)

Recursion (recursion)

There are three ways a function can point to and invoke itself:

1. Name of function

2.arguments.callee

3. A variable name in the scope that points to the function

Nested functions and closures (nested functions and closures)

You can nest another function inside a function. A nested (inner) function is private to its container (external) function. It also forms a closure (closure) itself. A closure is an expression (usually a function) that can own a separate environment and variable.

Since a nested function is a closure, it means that a nested function can "inherit" the parameters and variables of the container function. In other words, an intrinsic function contains the scope of an external function.

Can be summarized as follows:

    • Intrinsic functions can only be accessed in external functions.
    • The inner function forms a closure: it can access the parameters and variables of the external function, but the external function cannot use its arguments and variables.

JavaScript Learning Notes (a) function

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.