我試圖刪除一個數組的值,沒有刪除它的方法。請看下面的代碼:刪除一個變量的值而不丟失其子方法
var Project = function() {
//The array where all data will be stored before they are sent..
this.data = [];
// ... Along with a function to send data to other source ...
this.data.send = function() {
}
//Here is where the data would be altered ...
//Send the data ...
this.data.send();
//Remove the data, we don't want it when sending the next time ...
this.data = [];
// ... but this (obviously) results in the removal of the send() function ... :-(
}
這也將刪除該功能.send()
,這是不是我要找的行爲。什麼是最順暢和最合適的方式來躲避這個問題?謝謝!
如果將數組的長度設置爲零,會發生什麼?在這裏黑暗中射擊。 – 2012-03-08 22:21:31
你在數組中存儲函數的原因是什麼? – Eduardo 2012-03-08 22:24:35
@Eduardo這是一個非常好的問題。簡單地說:我沒有正確思考。 – Zar 2012-03-09 06:27:52