1
我正在使用一個API,它可以接受多個數組,我使用apply()
來傳遞數組,但數組包含的原型函數似乎無法從單個數組中移除。最終發生的是正在執行的原型功能。原型函數污染JavaScript數組
這是原型函數: if (!Array.prototype.last) { Array.prototype.last = function(){ return this[this.length - 1]; }}
例如當我登錄每個陣列我看到我EVENTLIST陣列以下:
["name1", "index1", "value1", last: function]
["name2", "index2", "value2", last: function]
["name3", "index3", "value3", last: function]
我然後按陣列:
apiName.push.apply(apiName, eventList);
我試過array.pop()
刪除每個陣列上的'最後'功能,但無濟於事。
原型函數如何在數組中最終結束,以及如何防止它在apply()中被調用?
感謝您的任何見解!
使用apply()還會運行那個原型函數嗎? – hybrid9 2013-05-07 16:26:20
不,'apply()'只運行你正在應用的函數。如果你用'concat()'代替它,可能會更容易? 'apiName = apiName.concat(eventList);' – Halcyon 2013-05-07 16:29:29