简体   繁体   English

涉及方法的 JavaScript 比较永远不会为真

[英]JavaScript comparison involving method is never true

I have the following JavaScript logical condition using the ternary operator:我有以下使用三元运算符的 JavaScript 逻辑条件:

var columnheader = (elem.getText === "ID") ? (Page.TableColumn(elem.getText())) : (Page.TableColumn(toTitleCase(elem.getText())));

For some reason when elem.getText value is 'ID' with no whitespace it doesn't evalute the first expression only the second, is there something wrong in my syntax?出于某种原因,当 elem.getText 值是没有空格的“ID”时它不会只计算第二个表达式,我的语法有问题吗? I've checked and double checked!我已经检查并仔细检查了!

getText is a function. getText 是一个函数。 In your code, you are comparing the function with "ID", not the result of the function call (getText() ).在您的代码中,您将函数与“ID”进行比较,而不是函数调用的结果 (getText() )。

Should be:应该:

var columnheader = (elem.getText() ==="ID") ? (Page.TableColumn(elem.getText())) : (Page.TableColumn(toTitleCase(elem.getText())));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM