【发布时间】:2013-12-06 21:38:48
【问题描述】:
这是我正在执行的代码:
filterIssues: function(objectKey, text){
var view = this;
var keys = objectKey.split(".");
var attributeKey = keys[0];
var attributeName;
if (keys.length > 1){
attributeName = keys[1];
}
view.issues.each(function(issue){
var value = issue.get(attributeKey);
console.log(text);
if (value === undefined || value === null){
issue.trigger("hide");
return;
}
if (attributeName !== undefined){
value = value[attributeName];
}
if(value !== undefined){
var matchedText = value.substring(0, text.length - 1);
if ( matchedText === text){
issue.trigger("show");
console.log(value);
return;
}
}
issue.trigger("hide");
});
}
matchedText == text 总是返回 false。
这是我在玩控制台时得到的:
> matchedText
"sande"
> text
"sande"
> typeof(text)
"string"
> typeof(matchedText)
"string"
> matchedText === text
false
> matchedText == text
false
我确实意识到=== 将始终检查两个对象是否相同并且我已阅读
JavaScript equal operations anomalies 和 Javascript string equality。
我忽略的代码有问题吗?
【问题讨论】:
-
检查
matchedText.length和text.length。 -
issues是什么类型的对象,issues.each()传递给函数的是什么(即issue是什么)?issue.get()返回什么? -
可能是栅栏张贴错误?
var matchedText = value.substring(0, text.length - 1);你试过没有 - 1 吗?matchedText.valueOf() === text.valueOf()或 with (==) 给你什么? -
你检查过 trim();
-
@James
matchedText.toString() == text.toString()返回false,===也是如此
标签: javascript