Sys.Debug assert 方法

更新:2007 年 11 月

检查条件,如果该条件为 false,则显示消息并提示用户中断至启动调试器。

Sys.Debug.assert(condition, message, displayCaller);

参数

  • condition
    如果条件为 true 则继续执行代码;如果条件为 false 则显示 message,并中断至启动调试器。

  • message
    (可选)要显示的消息。默认值为空字符串 ("")。

  • displayCaller
    (可选)true 指示调用 assert 的函数的名称应在消息中显示。默认值为 false。

备注

在代码中调用 assert 方法时,请以 true 或 false 来表示操作是否成功,并将该值用于 condition。如果操作失败(如果 condition 为 false),则执行断言逻辑。

assert 方法应该用于捕捉开发人员所犯的错误。若要响应用户错误和运行时错误条件(例如网络错误或权限失败),应引发一个异常。

对于不同的浏览器,调试行为、要求和跟踪消息的输出会有所不同。有关更多信息,请参见调试和跟踪 AJAX 应用程序概述

示例

下面的示例演示了一个函数,如果用户定义变量 n 不是正整数,该函数会调用 assert 方法。此示例摘自 Sys.Debug 类概述中的一个更大的示例。

function btnAssert_onclick() {
    var n;
    // Insert code intended to set n to a positive integer.
    if (false) n = 3;
    // Assert if n is not greater than 0.
    Sys.Debug.assert(n > 0, "n must be set to a positive integer.");
}
function btnAssert_onclick() {
    var n;
    // Insert code intended to set n to a positive integer.
    if (false) n = 3;
    // Assert if n is not greater than 0.
    Sys.Debug.assert(n > 0, "n must be set to a positive integer.");
}

请参见

概念

调试和跟踪 AJAX 应用程序概述

参考

Sys.Debug 类