JavaScript Functions Introduction _ Basics

Source: Internet
Author: User
Tags function definition pow

1. Definition and invocation of functions
in JavaScript, the most common way to define functions is to invoke a function statement. The statement is composed of a function keyword, it is followed by the following:
Ø function name
Ø A list of arguments with parentheses, arguments are optional, arguments are separated by commas
Ø include function bodies in curly braces
use functions to notice several places:
O If there is a return statement in the body of the function, it returns a value; if not, it returns undefined
Øjavascript does not detect whether the number of function arguments is correct, and if more arguments are passed than the formal parameters, the superfluous real arguments is not ignored; Then the parameters that are not passed are assigned to undefined, and in this case the general program will run an error

<script>//print1 Returns a value function Print1 () {return ' ddxkj '; //print1 no return statement, returns undefined function Print2 () {} function Print3 (STR1,STR2,STR3) {document.write (str1, "<br> ;"); document.write (str2, "<br>"); document.write (STR3, "<br>"); } document.write (Print1 (), "<br>"); document.write (Print2 (), "<br>"); Print3 (' AAA ', ' BBB ', ' CCC '); Same number of arguments and Parameters Print3 (' AAA ', ' BBB ', ' CCC ', ' ddd ', ' eee '); The number of arguments is more than that of the Print3 (' AAA ', ' BBB '); The number of arguments is less than the formal parameter </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
Ddxkj
Undefined
Aaa

Ccc
Aaa

Ccc
Aaa

Undefined
1.1. Nested functions
JavaScript 1.2 and ECMAScript v3 allow a function definition to appear in another function, which is nesting of functions. It should be noted that ECMAScript V3 does not allow function definitions to appear arbitrarily, they are still restricted to top-level global code and top-level function code, which means that function definitions cannot appear in loops and conditional statements. For example:
<script> function Print (a) {function square (x) {return x*x;} document.write (Square (a)); } print (100); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
10000
1.2. function () constructor
In ECMAScript V1 and JavaScript 1.1, you are allowed to dynamically define a function using function () and new. For example: var f = new Function ("X", "Y", "return x*y;");
The line code is almost equivalent to: function f (x,y) {return x*y}
The function () constructor can accept any number of string arguments, and the last argument is the body of the functions, which can contain any JAVSSCRPT statements, used between each line of statements; If you say the function has no arguments, just a string of the function body is OK.
Sometimes the function () constructor is also called an anonymous function, and there is no name defined.
Purpose of the function () constructor:
Ø Create and compile a function dynamically. Every time a function is called, the function () constructor compiles the functions once, which is a disadvantage. Therefore, the function () constructor is generally not used in loops or in functions that are often called.
Ø defines a function as a JavaScript expression, not a statement. But in JavaScript, if you want to define a function in an expression, rather than in a statement, it is more convenient to use the direct amount of the function.
1.3. Function Direct Quantity
The syntax of the direct volume of a function is very similar to a function statement, except that it is used as an expression, not a statement, and you can not specify a function name. As with function statements, function () constructors, the direct volume of functions is also one way to create a function. Such as:
Øfunction f (x) {return x*x;}//function statement
Øvar f = new funtion ("X", "Return x*x;"); function () constructor
Øvar f = function (x) {return x*x;};
Although the direct amount of the function creates an unnamed function, you can also specify the function name, which is written in the calling
Self recursive functions are useful. Such as:
var f = function fact (x) {if (x <= 1) return 1; else return x*fact (x-1);
The code above defines an unnamed function and stores its reference in F. It does not create the fact () function, but only allows the function body to use this name without invoking itself.
The direct amount of a function can be assigned, passed to a function, or even called directly, like a variable. For example:
<script> var f1 = function (x) {return x*x;}; Define and save var F2 = function (x) {return x*x;} (5); Direct call document.write (F1), "<br>"); document.write (F2, "<br>"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
100
25
2. Functions as Data
When a function is used as a data, it can be like a variable, assigned, stored in an object property or array, passed to a function.
When a function is defined as follows, function square (x) {return x*x}, defines a function object and assigns the function object to square. Square doesn't make any sense here, but it's just an object that stores a function object. You can assign this function to other variables. For example:
<script> function Square (x) {return x*x; var a = square (4); document.write (A, "<br>"); Print out var b = square; var C = B (10); document.write (c, "<br>"); Print out </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
16
100
<script> function Add (x,y) {return x + y;} function Subtract (x,y) {return x-y;} function Multiply (x,y) {return x * y;} function Divide (x,y) {return x/y;} function operate (operator, OPERAND1, Operand2) {return operator (OPERAND1, operand2); var i = operate (add, operate (Add, 2, 3), Operate (multiply, 4, 5)); document.write (i, "<br>"); var operators = new Object (); operators["Add" = function (x,y) {return x+y;}; operators["subtract"] = function (x,y) {return xy;}; operators["multiply"] = function (x,y) {return x*y;}; Operators["divide"] = function (x,y) {return x/y;}; operators["POW"] = Math.pow; function Operate2 (Op_name, Operand1, Operand2) {if (operators[op_name] = null) return "unknown operator"; else return Operators[op_name] (operand1, operand2); var j = operate2 ("Add", "Hello", Operate2 ("Add", "", "World")) var k = Operate2 ("Pow", 2) document.write (J, "< Br> "); document.write (k, "<br>"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
25
Hello World
100

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.