這裏是我執行的代碼:String對象的比較總是返回false
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.get()'返回什麼? – RobG
也許柵欄發佈錯誤? 'var matchedText = value.substring(0,text.length - 1);'你嘗試沒有 - 1? 'matchedText.valueOf()=== text.valueOf()'或者(==)會給你帶來什麼? – James