Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js代码,用于判断函数是否可用于对象_Javascript - Fatal编程技术网

Javascript js代码,用于判断函数是否可用于对象

Javascript js代码,用于判断函数是否可用于对象,javascript,Javascript,下面一行javascript得到一个未捕获的TypeError:对象#文本在chrome和safari中没有方法“getAttribute”,但在IE中没有 this.Element.getAttribute("whatever") 我知道这个.Element是主要的问题,但是我希望有一个临时的修复程序来调试代码的其他部分如何测试一个项上的函数是否可用而不出现javascript错误?您可以将其包装为try/catch?您可以将其包装为try/catch?请参阅@Jaspers-answer。

下面一行javascript得到一个未捕获的TypeError:对象#文本在chrome和safari中没有方法“getAttribute”,但在IE中没有

this.Element.getAttribute("whatever")

我知道这个.Element是主要的问题,但是我希望有一个临时的修复程序来调试代码的其他部分如何测试一个项上的函数是否可用而不出现javascript错误?

您可以将其包装为try/catch?您可以将其包装为try/catch?请参阅@Jaspers-answer。检查它是否未定义将防止您必须检查
this
this.element
@Bot:No,相反,如果您跳过这些检查并且未定义元素,JS引擎将抛出一个类型错误,类似于“无法读取undefinedSee@Jaspers answer的属性'getAttribute'。检查它是否未定义将防止您必须检查
this
this.element
@Bot:No,相反,如果您跳过这些检查并且未定义元素,JS引擎将抛出一个类型错误,类似于“无法读取未定义的属性'getAttribute',如果
此.Element.getAttribute
是布尔值该怎么办?”?或者除了函数之外的任何东西?@ScottSauyet听着,OP想要的只是测试这个对象是否存在.getattribute()。只要这个.Element是一个对象,这就可以工作。如果一个编码器将一个布尔命名为getAttribute,他不应该编码:)如果
这个.Element.getAttribute
是一个布尔呢?或者除了函数之外的任何东西?@ScottSauyet听着,OP想要的只是测试这个对象是否存在.getattribute()。只要这个.Element是一个对象,这就可以工作。如果编码器将布尔值命名为getAttribute,则不应进行编码:)
if (this.Element.getAttribute)
{
// exists
}
else
{
// does not
}
if (this && this.Element && typeof this.Element.getAttribute == "function") {
    // ...
}