当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript教程:JavaScript函数

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JavaScript教程:JavaScript函数


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

函数为程序设计人员提供了一个非常方便的能力。通常在进行一个复杂的程序设计时,总是根据所要完成的功能,将程序划分为一些相对独立的部分,每部分编写一个函数。从而,使各部分充分独立,任务单一,程序清晰,易懂、易读、易维护。JavaScript函数可以封装那些在程序中可能要多次用到的模块。并可作为事件驱动的结果而调用的程序。从而实现一个函数把它与事件驱动相关联。这是与其它语言不样的地方。

1、JavaScript函数定义
Function 函数名 (参数,变元)
{
   函数体;
   Return 表达式;
}
说明:
 当调用函数时,所用变量或字面量均可作为变元传递。
 函数由关键字Function定义。
 函数名:定义自己函数的名字。
 参数表,是传递给函数使用或操作的值,其值可以是常量 ,变量或其它表达式。
 通过指定函数名(实参)来调用一个函数。
 必须使用Return将值返回。
 函数名对大小写是敏感的。

2、函数中的形式参数:
  在函数的定义中,我们看到函数名后有参数表,这些参数变量可能是一个或几个。那么怎样才能确定参数变量的个数呢?在JavaScript中可通过arguments .Length来检查参数的个数。
例:

Function function_Name(exp1,exp2,exp3,exp4)
Number =function _Name . arguments .length;
if (Number>1

document.wrile(exp2);
if (Number>2)
document.write(exp3);
if(Number>3)
document.write(exp4);
...

3、函数调用方法

<html>
<head>
</head>
<body>
<script Language="JavaScript">
function f_name (a,b)  //定义函数
  {
   var c;
   c= a + b;
   return c;
  }
var m = f_name (10,5); //调用函数
document.write(m);  //输出值
</script>
</body>
</html>