当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 函数

JavaScript
JS数学对象参考
JS字符串对象参考
JS函数参考
JS事件参考
Javascript 常用正则表达式
FF和IE下的js兼容性问题
jQuery 简单介绍
jQuery / 核心 / $(expression, [context] ) 函数
jQuery / 核心 / $(html) 函数
如何使用JS来判断浏览器类型(ie、firefox,等等)
Javascript在IE和FireFox中的不同表现
3个js字符编码函数区别
javascript 中的 XMLDOM 对象

JavaScript 中的 JS 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1194 ::
收藏到网摘: n/a

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>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!")
}

</script>
</head>
<body>
<form>
<input type="button" value="Click me!"
onclick="displaymessage()" >

</form>
</body>
</html>

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)
{
some code }

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()
{
some code }

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)
{
x=a*b
return x
}

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中

评论 (3) 1 All

登陆 | 还没注册?