A function is a reusable code-block that will be executed by an event, or when the function is called.
函数是可再用的代码块,可以在事件触发或是被调用时来执行。
Examples
例子
Function
How to call a function.
怎样来调用一个函数
Function with arguments
How to pass a variable to a function, and use the variable in the function.
怎样为函数传递一个变量,并在函数中使用这个变量
Function with arguments 2
How to pass variables to a function, and use these variables in the function.
怎样为函数传递一个变量,并在函数中使用这个变量2
Function that returns a value
How to let the function return a value.
怎样使函数返回一个值
A function with arguments, that returns a value
How to let the function find the product of 2 arguments and return the result.
怎样让函数将传递给它的两个值经过处理后反馈出最后的结果
JavaScript Functions
JS函数
To keep the browser from executing a script as soon as the page is loaded, you can write your script as a function.
要想让浏览器在加载完页面后马上执行脚本程序,你可以将脚本写入一个函数内。
A function contains some code that will be executed only by an event or by a call to that function.
函数内的一些代码只有在某个事件触发或被调用的时候才会被执行。
You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file).
你可以在页面中的任何地方调用函数(可以用嵌入外部.js文件的方式让其他页面也可以使用脚本)
Functions are defined at the beginning of a page, in the <head> section.
函数在页面的开始部分定义,在<head>区域。
Example
例子
<html> <body> |
If the line: alert("Hello world!!"), in the example above had not been written within a function, it would have been executed as soon as the line was loaded. Now, the script is not executed before the user hits the button. We have added an onClick event to the button that will execute the function displaymessage() when the button is clicked.
如果上面例子里的alert("Hello world!!")这行没有写在函数里的话,它就会在这行被加载的时候就执行。现在,脚本在用户点击按钮前是不会执行的。我们需要添加一个onClick事件来让函数displaymessage()在按钮点击后执行
You will learn more about JavaScript events in the JS Events chapter.
你将在JS Events(事件)篇章中学到更多有关JS事件的内容
How to Define a Function
怎样来定义一个函数
The syntax for creating a function is:
建立函数的语法是这样的:
function functionname(var1,var2,...,varX) |
var1, var2, etc are variables or values passed into the function. The { and the } defines the start and end of the function.
var1,var2等一些变量或值可传递给函数使用。{和}定义了函数的开始与结束。
Note: A function with no parameters must include the parentheses () after the function name:
注意:没有参数的函数必须在函数名称后带上():
function functionname() |
Note: Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.
注意:请不要忘记JavaScript中书写要求的重要性!函数function必须使用小写字母,不然JavaScript就会出错!还有一点要注意的,你所调用的函数名必须和你建立的函数名相一致。
The return Statement
返回语句
The return statement is used to specify the value that is returned from the function.
返回语句用来指定从函数中返回的值
So, functions that are going to return a value must use the return statement.
所以,要从函数里返回值就必须使用返回语句
Example
例子
The function below should return the product of two numbers (a and b):
下面的函数就会返回两个数字的乘积(a和b):
function prod(a,b) |
When you call the function above, you must pass along two parameters:
当你调用上面的函数,你必须提供两个参数
product=prod(2,3) |
The returned value from the prod() function is 6, and it will be stored in the variable called product.
从函数prod()返回的值就为6,它会存储在变量product中