JS review-function (function of formal parameters and arguments are not organized, the method of the function to be supplemented, functional programming to expand)

Source: Internet
Author: User

Function

Definition of a function

Defined only once, but can be executed or invoked any time. Functions in JavaScript are parameterized: the definition of a function includes a list of identifiers called formal parameters that work like local variables in the body of a function. If a function is hung on an object, as a property of an object, it is called a method of an object. When the function is invoked through this object, the object is the context of the call, which is the value of this function's this. The function used to initialize a newly created object is called a constructor.

Three ways to define functions

1. Function declaration:

A function declaration statement actually declares a variable and copies a function object to it.

function sum (num1,num2) {    return num1 + num2;}

2. Function expression

With function expressions, you can implement dynamic programming without naming your functions. An anonymous function, also a lambda function, is a powerful way to use JavaScript functions.

Characteristics:

(1) Function expressions are different from function declarations. A function declaration requires a name, but the function expression does not need it. An expression without a name is also called an anonymous function.

(2) Recursive functions become more complex in situations where you cannot determine how to reference a function

(3) Recursive functions always call Arguments.callee to call themselves recursively, do not use function names-the functions name can change at any time

var sum = function (num1,num2)     return num1 + num2;};

If a function declaration expression contains a name, the local scope of the function will contain a name that is bound to the function object. At this point the name of the function becomes a local variable inside the function: for example, recursive invocation

var f = function fact (x) {if (x<=1) return 1; else return x*fact (x-1);}

function expressions are particularly well-suited for defining functions that are used only once:

var tensquared = (function (x) {return x*x;} (10));

The difference between the two methods of invocation:

Although function declaration statements and function definition expressions contain the same function names. But the two are still different. Both methods create a new function object, but the function declaration statement China's function name is a variable name, and the variable points to the function object. As with var declaration variables, function definition statements are displayed "ahead" to the top of the script or function body. So they are visible in both the script and the function body. With Var, only the variable declaration is in advance-the variable initialization code remains in its original position. However, with function declaration statements, function names and function bodies are in advance. That is, you can declare a JavaScript function before calling it.

3. Constructor function

Regardless of whether a function defines a statement or a function's direct expression, a function keyword is used to define functions, and a single function can also be defined using the function () constructor. The function () constructor can pass in any number of string arguments, and the text represented by the last argument is the body of the function, which can contain any JAVASCIRPT statement, separated by semicolons between each of the two statements.

Note: the function () constructor does not need to pass in arguments to specify the name of the functions. Just like the function direct quantity, the function () constructor creates an anonymous function.

var f = new Function ("X", "Y", "return x*y;"); var f = funciton (x, y) {return x*y;}

Third , constructors and common functions

The only difference between constructors and other functions is that they are called in different ways. However, constructors are functions after all, and there is no special syntax for defining constructors. Any function, as long as it is called by the new operator, can be used as a constructor, and any function, as long as it is not called by the new operator, has no difference from the normal function.

function Person (name,age,job) {     this.age = age;     this.name = name;     This.job = Job;}

1. Common functions

Person ("Greg", "Doctor"); Add to Windows

2. Constructor function

var person = new Person ("Nicholas", "Software Engineer");

What does the new operator do specifically:

(1) Create a new object

(2) assigns the scope of the constructor to the new object (so this points to the new object);

(3) The code that executes the constructor (adding attributes to the new object);

(4) Returning new objects

Four, return statement

The return statement causes the function to stop executing and returns its expression to the caller, returning undefined if there is no expression associated with it.

Five, function characteristics

Features: Declaration in advance

All variables declared in the JavaScript function are "advanced" to the top of the function body

Vi. Invocation of functions

1, as a function

Printprop ({x:1});

2. As a method

There is an important difference between a method call and a function call, that is (call context this), which can be referenced by this object in the body of the function.

Attention:

The method and the This keyword are at the heart of the object-oriented programming paradigm. Any function will actually pass in an implicit argument (this) as a method call-this argument is an object. It is important to note that this is a keyword, not a variable, or a property name. 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 wants to access this of the external function, it needs to keep the value of this in a variable, which is in the same scope as the inner function. This is usually saved with the variable self.

3, as a constructor function

The constructor can use the This keyword to refer to this newly created object

4. Calls through their call () and the Apply () method

Seven, closed bag

1. Definition: Closures are functions that have access to variables in another function scope. The way to create closures is to create another function inside one function. Variables inside the function body can be stored within the function scope, which is called "closures" in computer science literature. "Technically speaking, all

JavaScript functions are closures.

2, principle: In the background environment, the scope chain of the closure contains its own scope, the scope of the containing function and the global scope. Typically, the scope of a function and all of its variables are destroyed at the end of the function execution. However, when a function returns a closure, the scope of the function is persisted in memory until the closure does not exist.

3, Application: In the anonymous function and closure of the article is written to

4. Disadvantages

(1) Local variable resources returned in the scope of the closure are not immediately destroyed and recycled, so they may consume more memory. Excessive use of closures can result in degraded performance, and it is recommended that closures be used when necessary.

(2) Multiple closures defined in the same scope chain, sharing the same private variables or variables. When writing loop codes, errors often occur. Since a problem is caused by the mechanism of the domain chain, any variable obtained by the anonymous function in the loop is the last value

Viii. commonly used properties and methods

1. Properties

Length : The lengths of the function parameters

Arguments.length: Length of the function argument

Prototype: This property is a reference to an object called a prototype object. Each function contains a different prototype object. When a function is used as a constructor, the newly created object inherits the property from the prototype object.

2. Methods

Call ()

Apply ()

Bind ()

ToString ()

Ix. Custom Function Properties

A function in JavaScript is not a primitive value, but a special object. In other words, a function can have properties.

when a function requires a " Static " variable to keep a value constant, the most convenient way is to define a property for the function instead of defining a global variable. For example, suppose you want to write a function that returns a unique integer, regardless of where the calling function returns the integer, and the information needs to be persisted in a different function call process. This information can be put into global variables, but this is not necessary, because this information is only used by the function itself. It is best to save this information in one of the properties of the function object.

Ten, functional programming

JavaScript is not a functional programming language, but in JavaScript you can manipulate functions like objects, which means that functional programming techniques can be applied in JavaScript.

Example: Finding the mean and standard deviation of a number

var sum = function (x, y) {return x+y;}; var square = function (x) {return x*x;}; var data = [1,1,3,5,5];var mean = Data.map (function (x) {return x-mean;}); var deviations = Data.map (function (x) {return x-mean;}); var StdDev = math.sqrt (Deviations.map (square). reduce (sum)/(data.length-1));

  

  

JS review-function (function of formal parameters and arguments are not organized, the method of the function to be supplemented, functional programming to expand)

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.