有什么方法可以确定var是否是javascript中的elementFinder对象?

时间:2022-10-04 15:31:06

What is a way to determine if a var is an elementFinder object? The way I'm doing it now is to check if ".getText()" is part of the object. But I want to see if there's a better way.

确定var是否为elementFinder对象的方法是什么?我现在这样做的方法是检查“.getText()”是否是对象的一部分。但我想知道是否有更好的方法。

 if (!el.getText) { 
     throw "The supplied parameter is not an elementFinder and needs to be."; 
 }

2 个解决方案

#1


1  

Try this.

if(el.constructor !== elementFinder){
    throw "The supplied parameter is not an elementFinder and needs to be."; 
}

#2


1  

You can try using the instanceof operator.

您可以尝试使用instanceof运算符。

if( ! (el instanceof elementFinder) ){
    throw "The supplied parameter is not an elementFinder and needs to be."; 
}

#1


1  

Try this.

if(el.constructor !== elementFinder){
    throw "The supplied parameter is not an elementFinder and needs to be."; 
}

#2


1  

You can try using the instanceof operator.

您可以尝试使用instanceof运算符。

if( ! (el instanceof elementFinder) ){
    throw "The supplied parameter is not an elementFinder and needs to be."; 
}