PHP4 | Flash | ASP | CSS | VBScript | ADO | MySQL | JavaScript | Spring | Hibernate | win32api | 正则表达式 | 注册表 | xmlhttp .
当前位置:首页 → javascript上一篇 脚本问题解答    下一篇 创建高级对象
在当前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 教程
上一页
下一页



www.51chm.com
为什么要控制执行流程?
经常需要脚本在不同的条件下完成不同的事情。例如,您可以编写一个每小时检查时间的脚本,并在这一天中适当地更改某些参数。您也可以编写一个可以接收某种输入并采取相应操作的脚本。或者可以编写一个重复指定操作的脚本。

您可以测试若干种条件。Microsoft JScript 中所有的条件测试结果都是 Boolean 值,因此任何测试的结果是 truefalse。您可以自由地测试 Boolean 类型、数值类型或字符串类型的值。

JScript 提供了用于各种可能性的控制结构。最简单的控制结构是条件语句。

使用条件语句
JScript 支持 ifif...else 条件语句。在 if 语句中将测试一个条件,如果该条件满足测试,就将执行所编写的 JScript 编码。(在 if...else 语句中,如果条件不满足测试,则将执行不同的代码。)最简单的 if 语句格式可以在一行中写完,不过更常见的是多行的 ifif...else 语句。

下述示例演示了使用 ifif...else 语句的各种可能的语法。第一个示例演示了最简单的 Boolean 测试。当(且仅当)括号之间的项的值为 true 时,if 后续的语句或语句块才会被执行。

// smash() 函数是在该代码的其他地方定义的。
if (newShip)
   smash(champagneBottle,bow);  // Boolean 测试,看 newShip 是否为真。

// 在本示例中,除非两个条件都为真,否则该测试将不会被满足。
if (rind.color == "deep yellow " && rind.texture == "large and small wrinkles")
{
        theResponse = ("Is it a Crenshaw melon? <br> ");
}

// 在本示例中,只要任何一个条件为真,则测试即会满足。
var theReaction = "";
if ((lbsWeight > 15) || (lbsWeight > 45))
{
    theReaction = ("Oh, what a cute kitty! <br>");
}
else
    theReaction = ("That's one huge cat you've got there! <br>");
条件运算符
JScript 也支持隐式的条件格式。该格式在要测试的条件后使用一个问号(而不是在条件前的 if ),并指定两个可选项,一个在满足条件时使用,另一个在条件不满足时使用。两个选择项之间用一个冒号隔开。
var hours = "";

// 下面的代码指定 hours 是包含 theHour 的内容,
// 还是包含 theHour - 12 的内容。

hours += (theHour >= 12) ? " PM" : " AM";



www.51chm.com

提示   如果要一起测试多个条件,并且知道某个条件比其他条件更可能满足或不满足测试,(根据这些测试是用 OR (||) 还是 AND (&&) 连接的,可以通过将该条件放在条件语句的最前面来加速脚本的执行。例如,如果三个条件都必须为真(使用 && 运算符),并且第二个测试失败,则第三个条件不会被测试。

类似地,如果多个条件中只要有一个为真(使用 || 运算符),则当任何一个条件满足该测试时测试则停止。如果要测试的条件包括函数调用或其他代码,这种处理方式就特别有效。

快速判别的副作用的一个示例是:在下例中如果 runfirst() 返回 0 或 false, 则 runsecond 不会被执行。

if ((runfirst() == 0) || (runsecond() == 0))
// 若干代码  


www.51chm.com
使用重复或循环
有多种方式来重复执行一条语句或语句块。通常重复执行被称为 循环。 典型情况是用某个变量测试来进行控制,每执行一次循环变量的取值都会更改。 Microsoft JScript 支持很多种循环: for 循环、 for...in 循环、 while 循环、 do...while 循环和 switch 循环。
使用 for 循环
for 语句指定了一个计数器变量,一个测试条件,以及更新该计数器的操作。在每次执行循环之前(这称为一遍循环或一次重复),都将测试该条件。在执行该循环后,计算机变量将在下一次循环之前被更新。

如果循环条件永不会满足,则根本不会执行该循环。如果测试条件始终满足,则将导致无限循环。在有些情况下,前者可能是合乎需要的,而后者几乎没有用处,因此在编写循环条件时一定要小心。

/*
更新表达式 (下例中的 "icount++")将在循环结束时被执行,即在构成循环主体的语句块被执行后,在测试条件之前。
*/

var howFar = 11;  // 将循环次数限制为 11。

var sum = new Array(howFar);  // 创建一个称为 sum 并具有 11 个成员的数组,这 11 个成员从 0 到 10。
var theSum = 0;
sum[0] = 0;

for(var icount = 1; icount < howFar; icount++)  {        // 在本例中将从 1 到 10 进行计数。
theSum += icount;
sum[icount] = theSum;
}

var newSum = 0;
for(var icount = 1; icount > howFar; icount++)  {        // 该循环根本不会被执行。
newSum += icount;
}

var sum = 0;
for(var icount = 1; icount > 0; icount++)  {        // 这是一个无限循环。
sum += icount;
}
使用 for...in 循环
JScript 提供了一种特别的循环方式来遍历一个对象的所有属性。 for...in 循环中的循环计数器将遍历数组中的所有索引。它是一个字符串,而不是数字。
for (j in tagliatelleVerde)  // tagliatelleVerde 是一个有多个属性的对象
{
// 各种 JScript 代码语句。
}
使用 while 循环
while 循环与 for 循环非常相似。其不同之处是 while 循环没有内置的计数器或更新表达式。如果已经有由变量赋值反映的变化条件,并且希望使用该条件来控制语句或语句块的循环执行,就可以使用while 循环。
var theMoments = "";
var theCount = 42;        // 初始化计数器变量。
while (theCount >= 1)  {
    if (theCount > 1)  {
        theMoments = "Only " + theCount + " moments left!";
}
    else  {
        theMoments = "Only one moment left!";
    }
theCount--;        // 更新计数器变量。
}
theMoments = "BLASTOFF!";



www.51chm.com

注意  由于 while 循环没有显式的内置计数器变量,因此比其他类型更容易产生无限循环。此外,部分原因是不易发现循环条件是在何时何地被更新的,因此很容易编写了一个实际上从不更新条件的 while 循环。在编写 while 循环时应特别小心。



www.51chm.com
使用 breakcontinue 语句
Microsoft JScript 提供了一个语句来停止执行循环。break 语句可以用来在某些(特别)条件被满足时停止执行循环。continue 语句可以用来立即跳至下一次循环,即跳过余下的代码块并更新计数器变量,如 forfor...in 循环一样。
var theComment = "";
var theRemainder = 0;
var theEscape = 3;
var checkMe = 27;
for (kcount = 1; kcount <= 10; kcount++) 
{
    theRemainder = checkMe % kcount;
    if (theRemainder == theEscape)
      {
            break;  // 第一次遇到 remainder 等于 escape 时就从循环退出。
}
theComment = checkMe + " divided by " + kcount + " leaves a remainder of  " + theRemainder;
}

for (kcount = 1; kcount <= 10; kcount++) 
{
   theRemainder = checkMe % kcount;
   if (theRemainder != theEscape) 

   {
      continue;  // 只选择那些等于 escape 的 remainders,忽略其它。
}
// 使用选中的 remainders 的 JScript 代码。
}

var theMoments = "";
var theCount = 42;  // 初始化计数器。
while (theCount >= 1)  {
// if (theCount < 10)  {  // 警告!
// 这里使用 continue 将导致一个无限循环!。
// continue;
// }
    if (theCount > 1)  {
        theMoments = "Only " + theCount + " moments left!";
}
    else  {
        theMoments = "Only one moment left!";
    }
theCount--;  // 更新计数器。
}
theCount = "BLASTOFF!";


www.51chm.com
更多内容
·创建自己的对象
·的数据类型
·JScript 的变量
·FileSystemObject 词汇
·JScript 的函数
·JScript 词汇表
· 按字母顺序排列的关键字列表
·运算符操作
·concat 方法 (数组)
·concat 方法 (Array)
    首页 |  友情链接 |  网站地图      京ICP备07034540号    © 2007 www.51chm.com       Powered by xichy{at}163.com