PHP4 | Flash | ASP | CSS | VBScript | ADO | MySQL | JavaScript | Spring | Hibernate | win32api | 正则表达式 | 注册表 | xmlhttp .
当前位置:首页 → javascript上一篇 FileSystemObject 词汇    下一篇 JScript 词汇表
在当前chm中查找:
目录
·什么是 JScript?
·使用消息框
·固有对象
·脚本问题解答
·控制程序的流程
·创建高级对象
·创建自己的对象
·的数据类型
·JScript 的变量
·FileSystemObject 词汇
·JScript 的函数
·JScript 词汇表
· 按字母顺序排列的关键字列表
·运算符操作
·concat 方法 (数组)
·concat 方法 (Array)
·concat 方法 (Array)
·join 方法
·join 方法
·join 方法
·length 属性 (Array)
·length 属性 (Array)
·length 属性 (Array)
·reverse 方法
·reverse 方法
·reverse 方法
·slice 方法 (Array)
·slice 方法
·slice 方法
·sort 方法
·sort 方法
·sort 方法
·Drives 集合
·Drives 集合
·Drives 集合
·Drives 集合
·Files 集合
·Files 集合
·Files 集合
·Files 集合
·Folders 集合
·Folders 集合
·Folders 集合
·Folders 集合
·条件编译
·条件编译
·条件编译变量
·条件编译变量
·getDate 方法
·getDate 方法
·getDate 方法
·getDay 方法
·getDay 方法
·getDay 方法
·getFullYear 方法
·getFullYear 方法
·getFullYear 方法
·getHours 方法
·getHours 方法
·getHours 方法
·getMilliseconds 方法
·getMilliseconds 方法
·getMilliseconds 方法
·getMinutes 方法
·getMinutes 方法
·getMinutes 方法
·getMonth 方法
·getMonth 方法
·getMonth 方法
·getSeconds 方法
·getSeconds 方法
·getSeconds 方法
·getTime 方法
·getTime 方法
·getTime 方法
·getTimezoneOffset 方法
·getTimezoneOffset 方法
·getTimezoneOffset 方法
·getUTCDate 方法
·getUTCDate 方法
·getUTCDate 方法
·getUTCDay 方法
·getUTCDay 方法
·getUTCDay 方法
·getUTCFullYear 方法
·getUTCFullYear 方法
·getUTCFullYear 方法
·getUTCHours 方法
·getUTCHours 方法
·getUTCHours 方法
·getUTCMilliseconds 方法
·getUTCMilliseconds 方法
·getUTCMilliseconds 方法
·getUTCMinutes 方法
·getUTCMinutes 方法
·getUTCMinutes 方法
·getUTCMonth 方法
·getUTCMonth 方法
·getUTCMonth 方法
·getUTCSeconds 方法
·getUTCSeconds 方法
·getUTCSeconds 方法
·getVarDate 方法
·getVarDate 方法
·getVarDate 方法
·getYear 方法
·getYear 方法
·getYear 方法
·parse 方法
·parse 方法
·parse 方法
·setDate 方法
·setDate 方法
·setDate 方法
·setFullYear 方法
·setFullYear 方法
·setFullYear 方法
·setHours 方法
·setHours 方法
·setHours 方法
·setMilliseconds 方法
·getMilliseconds 方法
·setMilliseconds 方法
·setMinutes 方法
·setMinutes 方法
· setMinutes 方法
·setMonth 方法
·setMonth 方法
·setMonth 方法
·setSeconds 方法
·setSeconds 方法
·setSeconds 方法
·setTime 方法
·setTime 方法
·setTime 方法
·setUTCDate 方法
·setUTCDate 方法
·setUTCDate 方法
·setUTCFullYear 方法
·setUTCFullYear 方法
·setUTCFullYear 方法
·setUTCHours 方法
·setUTCHours 方法
·setUTCHours 方法
·setUTCMilliseconds 方法
·setUTCMilliseconds 方法
·setUTCMilliseconds 方法
·setUTCMinutes 方法
·setUTCMinutes 方法
·setUTCMinutes 方法
·setUTCMonth 方法
·setUTCMonth 方法
·setUTCMonth 方法
·setUTCSeconds 方法
·setUTCSeconds 方法
·setUTCSeconds 方法
·setYear 方法
·setYear 方法
·setYear 方法
·toGMTString 方法
·toGMTString 方法
·toGMTString 方法
·toLocaleString 方法
·toLocaleString 方法
·toLocaleString 方法
·toUTCString 方法
·toUTCString 方法
·toUTCString 方法
·UTC 方法
·UTC 方法
·UTC 方法
·Add 方法 (Dictionary)
·Add 方法 (Dictionary)
·CompareMode 属性
·CompareMode 属性
·CompareMode 属性
·Count 属性
·Count 属性
·Count 属性
·Exists 方法
·Exists 方法
·Exists 方法
·Item 属性
·Item 属性
·Item 属性
·Items 方法
·Items 方法
·Items 方法
·Key 属性
·Key 属性
·Key 属性
·Keys 方法
·Keys 方法
·Keys 方法
·Remove 方法
·Remove 方法
·RemoveAll 方法
·RemoveAll 方法
·RemoveAll 方法
·Remove 方法
·AvailableSpace 属性
·AvailableSpace 属性
·AvailableSpace 属性
·DriveLetter 属性
·DriveLetter 属性
·DriveLetter 属性
·DriveType 属性
·DriveType 属性
·DriveType 属性
·FileSystem 属性
·FileSystem 属性
·FileSystem 属性
·FreeSpace 属性
·FreeSpace 属性
·FreeSpace 属性
·IsReady 属性
·IsReady 属性
·IsReady 属性
·Path 属性
·Path 属性
·Path 属性
·RootFolder 属性
·RootFolder 属性
·RootFolder 属性
·SerialNumber 属性
·SerialNumber 属性
·SerialNumber 属性
·ShareName 属性
·ShareName 属性
·ShareName 属性
·TotalSize 属性
·TotalSize 属性
·TotalSize 属性
·VolumeName 属性
·VolumeName 属性
·VolumeName 属性
·atEnd 方法
·atEnd 方法
·atEnd 方法
·item 方法
·item 方法
·item 方法
·moveFirst 方法
·moveFirst 方法
·moveFirst 方法
·moveNext 方法
·moveNext 方法
·moveNext 方法
· JScript 错误
· 特性信息
·Attributes 属性
·Attributes 属性
·Attributes 属性
·Copy 方法
·Copy 方法
·Copy 方法
·CreateTextFile 方法
·CreateTextFile 方法
·CreateTextFile 方法
·DateCreated 属性
·DateCreated 属性
·DateCreated 属性
·DateLastAccessed 属性
·DateLastAccessed 属性
·DateLastAccessed 属性
·DateLastModified 属性
·DateLastModified 属性
·DateLastModified 属性
·Delete 方法
·Delete 方法
·Delete 方法
·Drive 属性
·Drive 属性
·Drive 属性
·Move 方法
·Move 方法
·Move 方法
·Name 属性
· Name 属性
·Name 属性
·OpenAsTextStream 方法
·OpenAsTextStream 方法
·OpenAsTextStream 方法
·OpenTextFile 方法
·OpenTextFile 方法
·OpenTextFile 方法
·ParentFolder 属性
·ParentFolder 属性
·ParentFolder 属性
·ShortName 属性
·ShortName 属性
·ShortName 属性
·ShortPath 属性
·ShortPath 属性
·ShortPath 属性
·Size 属性
·Size 属性
·Size 属性
·BuildPath 方法
·BuildPath 方法
·BuildPath 方法
·CopyFile 方法
·CopyFile 方法
·CopyFile 方法
·CopyFolder 方法
·CopyFolder 方法
·CopyFolder 方法
·CreateFolder 方法
·CreateFolder 方法
·CreateFolder 方法
·DeleteFile 方法
·DeleteFile 方法
·DeleteFile 方法
·DeleteFolder 方法
·DeleteFolder 方法
·DeleteFolder 方法
·DriveExists 方法
·DriveExists 方法
·DriveExists 方法
·Drives 属性
·Drives 属性
·Drives 属性
·FileExists 方法
·FileExists 方法
·FileExists 方法
·FolderExists 方法
·FolderExists 方法
·FolderExists 方法
·GetAbsolutePathName 方法
·GetAbsolutePathName 方法
·GetAbsolutePathName 方法
·GetBaseName 方法
·GetBaseName 方法
·GetBaseName 方法
·GetDrive 方法
·GetDrive 方法
·GetDrive 方法
·GetDriveName 方法
·GetDriveName 方法
·GetDriveName 方法
·GetExtensionName 方法
·GetExtensionName 方法
·GetExtensionName 方法
·GetFile 方法
·GetFile 方法
·GetFile 方法
·GetFileName 方法
·GetFileName 方法
·GetFileName 方法
·GetFileVersion 方法
·GetFileVersion 方法
·GetFileVersion 方法
·GetFolder 方法
·GetFolder 方法
·GetFolder 方法
·GetParentFolderName 方法
·GetParentFolderName 方法
·GetParentFolderName 方法
·GetSpecialFolder 方法
·GetSpecialFolder 方法
·GetSpecialFolder 方法
·GetTempName 方法
·GetTempName 方法
·GetTempName 方法
·MoveFile 方法
·MoveFile 方法
·MoveFile 方法
·MoveFolder 方法
·MoveFolder 方法
·MoveFolder 方法
·Type 属性
·Type 属性
·Type 属性
·GetObject 函数
·GetObject 函数
·ScriptEngine 函数
·ScriptEngineBuildVersion 函数
·ScriptEngineBuildVersion 函数
·ScriptEngine 函数
·ScriptEngineMajorVersion 函数
·ScriptEngineMajorVersion 函数
·ScriptEngineMinorVersion 函数
·ScriptEngineMinorVersion 函数
·Add 方法 (Folders)
·Add 方法 (Folders)
·Add 方法 (Folders)
·Files 属性
·Files 属性
·Files 属性
·IsRootFolder 属性
·IsRootFolder 属性
·IsRootFolder 属性
·SubFolders 属性
·SubFolders 属性
·SubFolders 属性
·Microsoft(R) JScript 运行时库参考
·JScript 教程
· 函数
·arguments 属性
·arguments 属性
·arguments 属性
·caller 属性
·caller 属性
·caller 属性
·length 属性 (Function)
·length 属性
·length 属性 (Function)
·escape 方法
·escape 方法
·escape 方法
·eval 方法
·eval 方法
·eval 方法
·Infinity 属性
·Infinity 属性
·Infinity 属性
·isFinite 方法
·isFinite 方法
·isFinite 方法
·isNaN 方法
·isNaN 方法
·isNaN 方法
·NaN 属性 (Global)
·NaN 属性
·NaN 属性
·parseFloat 方法
·parseFloat 方法
·parseFloat 方法
·parseInt 方法
·parseInt 方法
·parseInt 方法
·unescape 方法
·unescape 方法
·unescape 方法
·JScript 比较运算符
·比较运算符
·Microsoft JScript 特性 - ECMA
·Microsoft JScript 特性 - 非-ECMA
·Microsoft Scripting 运行时特性
·abs 方法
·abs 方法
·abs 方法
·acos 方法
·acos 方法
·acos 方法
·asin 方法
·asin 方法
·asin 方法
·atan 方法
·atan2 方法
·atan2 方法
·atan2 方法
·atan 方法
·atan 方法
·ceil 方法
·ceil 方法
·ceil 方法
·cos 方法
·cos 方法
·cos 方法
·E 属性
·E 属性
·E 属性
·exp 方法
·exp 方法
·exp 方法
·floor 方法
·floor 方法
·floor 方法
·LN10 属性
·LN10 属性
·LN10 属性
·LN2 属性
·LN2 属性
·LN2 属性
·log 方法
·LOG10E 属性
·LOG10E 属性
·LOG10E 属性
·LOG2E 属性
·LOG2E 属性
·LOG2E 属性
·log 方法
·log 方法
·max 方法
·max 方法
·max 方法
·min 方法
·min 方法
·min 方法
·PI 属性
·PI 属性
·pow 方法
·pow 方法
·pow 方法
·random 方法
·random 方法
·random 方法
·round 方法
·round 方法
·round 方法
·sin 方法
·sin 方法
·sin 方法
·sqrt 方法
·SQRT1_2 属性
·SQRT1_2 属性
·SQRT1_2 属性
·SQRT2 属性
·SQRT2 属性
·SQRT2 Property
·sqrt 方法
·sqrt 方法
·tan 方法
·tan 方法
·tan 方法
· 方法
·JScript 运行时错误
·JScript 语法错误
·MAX_VALUE 属性
·MAX_VALUE 属性
·MAX_VALUE 属性
·MIN_VALUE 属性
·MIN_VALUE 属性
·MIN_VALUE 属性
·NaN 属性
·NaN 属性
·NaN 属性
·NEGATIVE_INFINITY 属性
·NEGATIVE_INFINITY 属性
·NEGATIVE_INFINITY 属性
·POSITIVE_INFINITY 属性
·POSITIVE_INFINITY 属性
·POSITIVE_INFINITY 属性
· 对象
·ActiveXObject 对象
·ActiveXObject 对象
·Array 对象
·Array 对象
·Array 对象方法
·Array 对象属性
·Boolean 对象
·Boolean 对象
·Boolean 对象方法
·Boolean 对象属性
·Date 对象
·Date 对象
·Date 对象方法
·Date 对象属性
·Dictionary 对象
·Dictionary 对象
·Dictionary 对象方法
·Dictionary 对象属性
·Drive 对象
·Drive 对象
·Drive 对象
·Drive 对象
·constructor 属性
·constructor 属性
·constructor 属性
·prototype 属性
·prototype 属性
·prototype 属性
·toString 方法
·toString 方法
·toString 方法
·valueOf 方法
·valueOf 方法
·valueOf 方法
·Enumerator 对象
·Enumerator 对象
·Enumerator 对象
·Enumerator 对象
·Error 对象
·Error 对象
·Error 对象
·File 对象
·File 对象
·File 对象
·File 对象
·FileSystemObject 对象
·FileSystemObject 对象
·FileSystemObject 对象方法
·FileSystemObject 对象属性
·Folder 对象
·Folder 对象
·Folder 对象
·Folder 对象
·Function 对象
·Function 对象
·Function 对象方法
·Function 对象属性
·Global 对象
·Global 对象
·Global 对象
·Global 对象
·Math 对象
·Math 对象
·Math 对象方法
·Math 对象属性
·Number 对象
·Number 对象
·Number 对象方法
·Number 对象属性
·Object 对象
·Object 对象
·Object 对象
·Object 对象
·RegExp 对象
·RegExp 对象
·RegExp 对象
·RegExp 对象
·正则表达式对象
·正则表达式对象
·正则表达式对象
·正则表达式对象
·String 对象
·String 对象
·String 对象方法
·String 对象属性
·TextStream 对象
·TextStream 对象
·TextStream 对象
·TextStream 对象
·VBArray 对象
·VBArray 对象
·VBArray 对象
·VBArray 对象
· 运算符
·运算符优先级
·JScript 的运算符
·加法运算符 (+)
·加法运算符 (+)
·加法赋值运算符 (+=)
·加法赋值运算符 (+=)
·复合赋值运算符
·逻辑“与”运算符 (&&)
·逻辑“与”运算符 (&&)
·赋值运算符 (=)
·赋值运算符 (=)
·按位“与”赋值运算符 (&=)
·按位“与”赋值运算符 (&=)
·按位“或”赋值运算符 (|=)
·按位“或”赋值运算符 (|=)
·按位“与”运算符 (&)
·按位“与”运算符 (&)
·按位“非”运算符 (~)
·按位“非”运算符 (~)
·按位“或”运算符 (|)
·按位“或”运算符 (|)
·逗号运算符 (,)
·逗号运算符(,)
·条件(三目)运算符 (?:)
·条件(三元)运算符 (?:)
·delete 运算符
·delete 运算符
·除法赋值运算符 (/=)
·除法赋值运算符 (/=)
·除法运算符 (/)
·除法运算符 (/)
·递增 (++) 和递减 (--) 运算符
·递增 (++) 和递减 (--) 运算符
·instanceof 运算符
·instanceof 运算符
·运算符总结
·左移赋值运算符 (<<=)
·左移赋值运算符 (<<=)
·按位左移运算符 (<<)
·按位左移运算符 (<<)
·取余运算符 (%)
·取余赋值运算符 (%=)
·取余赋值运算符 (%=)
·乘法赋值运算符 (*=)
·乘法赋值运算符 (*=)
·乘法运算符 (*)
·乘法运算符 (*)
·new 运算符
·逻辑“非”运算符 (!)
·逻辑“非”运算符 (!)
·逻辑“或”运算符 (||)
·逻辑“或”运算符 (||)
·右移赋值运算符 (>>=)
·右移赋值运算符 (>>=)
·按位右移运算符 (>>)
·按位右移运算符 (>>)
·减法赋值运算符 (-=)
·减法赋值运算符 (-=)
·减法运算符 (-)
·减法运算符 (-)
·typeof 运算符
·typeof 运算符
·无符号右移赋值操作 (>>>=)
·无符号右移赋值运算符 (>>>=)
·无符号右移运算符 (>>>)
·无符号右移运算符 (>>>)
·void 运算符
·void 运算符
·按位“异或”运算符 (^)
·按位“异或”运算符 (^)
·按位“异或”赋值运算符 (^=)
·按位“异或”赋值运算符 (^=)
· 属性
·description 属性
·description 属性
·description 属性
·number 属性
·number 属性
·number 属性
·index 属性
·index 属性
·index 属性
·input 属性
·input 属性
·input 属性
·lastIndex 属性
·lastIndex 属性
·lastIndex 属性
·$1...$9 属性
·$1..$9 属性
·$1...$9 属性
·compile 方法
·compile 方法
·compile 方法
·exec 方法
·exec 方法
·exec 方法
·lastIndex 属性(正则表达式)
·lastIndex 属性(正则表达式)
·lastIndex 属性(正则表达式)
·source 属性
·source 属性
·source 属性
·test 方法
·test 方法
·test 方法
·正则表达式的语法
·正则表达式的语法
·正则表达式的语法
·JScript 的保留关键字
· 字母顺序的关键字列表
· 特性信息
· 方法
· 对象
· 属性
· 语句
·break 语句
·break 语句
·@cc_on 语句
·@cc_on 语句
·Comment 语句
·Comment 语句
·@if 语句
·@if 语句
·continue 语句
·continue 语句
·do...while 语句
·do...while 语句
·for 语句
·for 语句
·for...in 语句
·for...in 语句
·function 语句
·function 语句
·if...else 语句
·if...else 语句
·Labeled 语句
·Labeled 语句
·return 语句
·return 语句
·@set 语句
·switch 语句
·switch 语句
·this 语句
·this 语句
·throw 语句
·throw 语句
·try...catch 语句
·try...catch 语句
·var 语句
·var 语句
·while 语句
·while 语句
·with 语句
·with 语句
·anchor 方法
·anchor 方法
·anchor 方法
·big 方法
·big 方法
·big 方法
·blink 方法
·blink 方法
·blink 方法
·bold 方法
·bold 方法
·bold 方法
·charAt 方法
·charAt 方法
·charAt 方法
·charCodeAt 方法
·charCodeAt 方法
·charCodeAt 方法
·concat 方法(String)
·concat 方法(字符串)
·concat 方法(字符串)
·fixed 方法
·fixed 方法
·fixed 方法
·fontcolor 方法
·fontcolor 方法
·fontcolor 方法
·fontsize 方法
·fontsize 方法
·fontsize 方法
·fromCharCode 方法
·fromCharCode 方法
·fromCharCode 方法
·indexOf 方法
·indexOf 方法
·indexOf 方法
·italics 方法
·italics 方法
·italics 方法
·lastIndexOf 方法
·lastIndexOf 方法
·lastIndexOf 方法
·length 属性(字符串)
·length 属性(String)
·length 属性(String)
·link 方法
·link 方法
·link 方法
·match 方法
·match 方法
·match 方法
·replace 方法
·replace 方法
·replace 方法
·search 方法
·search 方法
·search 方法
·slice 方法 (String)
·slice 方法 (String)
·slice 方法 (String)
·small 方法
·small 方法
·small 方法
·split 方法
·split 方法
·split 方法
·strike 方法
·strike 方法
·strike 方法
·sub 方法
·sub 方法
·sub 方法
·substr 方法
·substr 方法
·substr 方法
·substring 方法
·substring 方法
·substring 方法
·sup 方法
·sup 方法
·sup 方法
·toLowerCase 方法
·toLowerCase 方法
·toLowerCase 方法
·toUpperCase 方法
·toUpperCase 方法
·toUpperCase 方法
·AtEndOfLine 属性
·AtEndOfLine 属性
·AtEndOfLine 属性
·AtEndOfStream 属性
·AtEndOfStream 属性
·AtEndOfStream 属性
·Close 方法
·Close 方法
·Close 方法
·Column 属性
·Column 属性
·Column 属性
·Line 属性
·Line 属性
·Line 属性
·Read 方法
·Read 方法
·ReadAll 方法
·ReadAll 方法
·ReadAll 方法
·Read 方法
·ReadLine 方法
·ReadLine 方法
·ReadLine 方法
·Skip 方法
·Skip 方法
·Skip 方法
·SkipLine 方法
·SkipLine 方法
·SkipLine 方法
·Write 方法
·Write 方法
·WriteBlankLines 方法
·WriteBlankLines 方法
·WriteBlankLines 方法
·Write 方法
·WriteLine 方法
·WriteLine 方法
·WriteLine 方法
·Microsoft(R) JScript 语言参考
·JScript 教程
·JScript 教程
·数据类型转换
·dimensions 方法
·dimensions 方法
·dimensions 方法
·getItem 方法
·getItem 方法
·getItem 方法
·lbound 方法
·lbound 方法
·lbound 方法
·toArray 方法
·toArray 方法
·toArray 方法
·ubound 方法
·ubound 方法
·ubound 方法
·版本信息
·JScript 的对象
·递归
·变量范围
·FileSystemObject 对象
·FileSystemObject 示例代码
·Microsoft(R) Scripting 库 - FileSystemObject 和 Scripting 运行时库参考的介绍
·设计 FileSystemObject
·Microsoft(R) Scripting 库 - FileSystemObject 对象
·处理驱动器和文件夹
·处理文件
·特殊字符
·编写 JScript 代码
·使用数组
·复制、传递和比较数据
·在浏览器中显示信息
JScript 的函数

JScript 的函数
 JScript 教程
上一页
下一页



www.51chm.com
什么是函数?
Microsoft JScript 的函数执行各种操作。函数还可以返回结果。有时是计算或比较的结果。

函数将若干操作合并在一个名字下。这样就可以简化代码。可以编写一组语句为其命名,然后通过调用该语句组并给其传递所需的任何信息,根据需要随时执行整个语句组。

给函数传递信息的方式是在函数名后用圆括号将信息括起来。要传递给某个函数的信息的各部分被称为参数。有些函数不需要任何参数;有些函数可能要带一个参数;有些可能有多个。甚至有一些函数的参数个数取决于使用该函数的方式。

JScript 支持两种函数:即在语言中内置的函数,以及自己创建的函数。

特殊的内置函数
JScript 语言中包括若干内置的函数。有些可以用来处理表达式和特殊字符,以及将字符串转换为数值。

例如,escape()unescape() 用来转换 HTML 代码中具有特殊意义的字符、不能直接输入到文字的字符。例如,尖括号 "<" 和 ">",描述了 HTML 标识。

escape 函数将任何特殊字符作为其参数,并返回代表该字符的转义码。每个转义码由两个数字以及后跟的一个百分号 (%) 组成。unescape 函数就是对应的逆函数。它的参数为由两个数字和一个百分号组成的字符串,并返回一个字符。

另一个有用的内置函数是 eval(),该函数对用字符串形式表示的任何合法数学表达式求值。eval() 函数有一个参数,即要求值的表达式。

var anExpression = "6 * 9 % 7";
var total = eval(anExpression);        // 将 5 赋给变量 total。
var yetAnotherExpression = "6 * (9 % 7)";
total = eval(yetAnotherExpression)        // 将 12 赋给变量 total。

var totality = eval("...surrounded by acres of clams.");        // 产生一个错误。
有关这些以及其他内置函数的详细信息,请参阅 语言参考
创建自己的函数
可以创建自己的函数并在需要的地方使用。一个函数定义由一个 function 语句和一个 JScript 语句块组成。

下面示例中的 checkTriplet 函数将三角形的边长作为其参数,然后通过检查这三个数是否能组成一个 Pythagorean (毕达哥拉斯)三元组(直角三角形斜边长的平方等于其它两条边长的平方和。),从而计算出该三角形是否为直角三角形。 checkTriplet 函数调用另两个函数中的其中一个来进行实际测试。

请注意使用了非常小的数 (“epsilon”) 作为该测试的浮点数版本的一个测试变量。因为浮点数计算不精确和存在舍入误差,因此直接判断斜边的平方是否等于另两边的平方和是不实际的,除非已知该问题中的三个值都是整数。由于直接测试更精确,因此该示例中的代码先判断这个数是否适当,如果是,再使用。

var epsilon = 0.0000000000001;  // 用于测试的非常小的数。
var triplet = false;

function integerCheck(a, b, c)  {  // 适用于整数的检验函数。
    if ( (a*a) == ((b*b) + (c*c)) )  {  // 具体的检验代码。
    triplet = true;
    }
}  // 整数判别函数结束。

function floatCheck(a, b, c)  {  // 适用于浮点数的检验函数。
var theCheck = ((a*a) - ((b*b) + (c*c)))  // 求检验数。
    if (theCheck < 0)  {  // 判别需要使用绝对值,如果 theCheck 为负数,先转为相应的正数。
    theCheck *= -1;
    }
    if (epsilon > theCheck)  {  // 如果足够接近非常小值,则可以认为满足条件!
    triplet = true;
    }
}  // 浮点数判别函数结束。

function checkTriplet(a, b, c)  {  // 三元组判别函数。首先将最长边设为 "a"。
var d = 0;  // 创建一个临时存放变量。
    if (c > b)  {  // 如果 c 大于 b,两者进行交换。
    d = c;
    c = b;
    b = d;
    }  // 否则无需操作。
    if (b > a)  {  // 如果 b 大于 a,两者进行交换。
    d = b;
    b = a;
    a = d;
    }  // 否则无需操作。

// 如果是直角三角形,则 "a" 边现在就是斜边。

    if (((a%1) == 0) && ((b%1) == 0) && ((c%1) == 0))  {  // 测试所有 3 个值是否均为整数?
    integerCheck(a, b, c);  // 如果是,则使用精确判别。
    }
    else
        floatCheck(a, b, c);  // 如果不是,则求最可能的结果。
}  // 三元组判别函数结束。

// 下面三条语句指定示例值,以进行测试。
var sideA = 5;
var sideB = 5;
var sideC = Math.sqrt(50);

checkTriplet(sideA, sideB, sideC);  // 调用该函数。在调用后, triplet 即包含了结果。


www.51chm.com
更多内容
· 按字母顺序排列的关键字列表
·运算符操作
·concat 方法 (数组)
·concat 方法 (Array)
·concat 方法 (Array)
·join 方法
·join 方法
·join 方法
·length 属性 (Array)
·length 属性 (Array)
    首页 |  友情链接 |  网站地图      京ICP备07034540号    © 2007 www.51chm.com       Powered by xichy{at}163.com