2013-11-14 13 views
1

如果我用這個代碼,而原型框架:爲什麼array.prototype.map.call(..)在使用那裏的原型框架的情況下不起作用?

if (XMLHttpRequest.prototype.sendAsBinary) return; 
     XMLHttpRequest.prototype.sendAsBinary = function(datastr) { 
      function byteValue(x) { 
       return x.charCodeAt(0) & 0xff; 
      } 
      console.log(Array.prototype.map); 
      var ords = Array.prototype.map.call(datastr, byteValue); 
      var ui8a = new Uint8Array(ords); 
      this.send(ui8a.buffer); 
     } 

數收益:

function map() { [native code] } 

如果原型JS框架中包括然後登錄返回此一個:

function collect(iterator, context) { 
    iterator = iterator || Prototype.K; 
    var results = []; 
    this.each(function(value, index) { 
     results.push(iterator.call(context, value, index)); 
    }); 
    return results; 
    } 

同時我遇到了一個錯誤:

Uncaught TypeError: Object [object String] has no method 'each' prototype.js:864 
collect prototype.js:864 
XMLHttpRequest.sendAsBinary jquery.filedrop.js:309 
send jquery.filedrop.js:215 

無論如何,jQuery也在使用。

jQuery.noConflict(); 

爲什麼我不能在原型框架打開的情況下運行本地映射函數?

+1

是'datastr'數組? (答案是否定)原型用自己的方法覆蓋'Array.prototype.map'。該方法似乎不適用於字符串。 –

+0

我想你可以通過將字符串設置爲一個數組''datastr.split(「」)'來修復它,在這一點上,你可以簡單地做'var ords = datastr.split(「」)。map(byteValue) –

+0

datastr是一個圖像字符串數據。 – Anthony

回答

2

當您包含prototypejs時,將Array.prototype.map替換爲prototypejs方法。新方法假定它總是在數組上調用,這會導致你的用例失敗,因爲你在一個字符串上調用它。

解決方法是將字符串轉換爲數組。

var ords = Array.prototype.map.call(datastr.splt(""), byteValue); 

然後可以簡化爲:

var ords = datastr.splt("").map(byteValue); 
相關問題