0

我有一个功能,说:

function some_name( that ) {
// do something here
}

这个函数在 2 个地方被调用。位置 1 位于 document.ready 位置,位置 2 位于单击按钮时。

所以:

// document.ready
function some_name();

...

// button click
function some_name( $(this) );

现在回到函数本身,如何检查是否that已设置?

4

2 回答 2

2

一种选择是使用typeof

function some_name(that) {
    if (typeof that != "undefined") {
        // do smth with that
    } else {
        // do smth without that
    }
}

另一种选择是使用arguments

function some_name(that) {
    if (arguments.length) {
        // do smth with that
    } else {
        // do smth without that
    }
}

如果您还需要检查$(this)页面上是否存在元素,您可以使用length

if (that.length) {
    // element exists
} else {
    // element does not exist
}
于 2012-05-14T12:04:41.140 回答
1
function some_name(that) {
    if (that != undefined) {
        alert('that is good!');
    }
}
于 2012-05-14T12:02:58.233 回答