我正在通過peepcode jQuery示例(https://peepcode.com/products/jquery),我選擇更新所有依賴關係。JQuery.each將字符串文字轉換爲字符串。爲什麼?
不足爲奇的是,我遇到了一個問題,關於字符串和String對象之間的JavaScript(大討論在這裏:Why are there two kinds of JavaScript strings?)的區別
一個問題我打的是jQuery.each字符串文字轉換爲字符串jQueryUI方法removeClass無法正確處理的對象。這是修復問題的代碼片段。
我想知道如果錯誤是在jQuery.each錯誤地將字符串文字轉換爲字符串對象或應該removeClass假設它可以得到一個字符串文字或字符串對象。
更新:我剛剛意識到這是另一種解決辦法: Strings in array are no longer strings after jQuery.each()
下面的代碼:
jQuery.fn.extend({
taskStates:["task-empty", "task-x", "task-apostrophe", "task-dash"],
resetTaskStateClassNames: function() {
var elements = this;
// Commented code breaks as "this" becomes the String object not the literal
// and removeClass does not check for String Object
// jQuery.each(jQuery.fn.taskStates, function(){
// elements.removeClass(this); // this is the taskState
// })
jQuery.fn.taskStates.forEach(function(ts) {
elements.removeClass(ts);
});
return this;
},