为什么array.prototype.map.call(..)在原型框架使用的情况下不起作用?

时间:2013-11-14 20:03:34

标签: javascript jquery map prototypejs prototype

如果我在没有原型框架的情况下使用此代码:

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框架,那么log将返回以下内容:

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 个答案:

答案 0 :(得分:2)

当您包含prototypejs时,Array.prototype.map将替换为prototypejs方法。新方法假设它将始终在数组上调用,这会导致您的用例失败,因为您在字符串上调用它。

解决方法是将字符串转换为数组。

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

然后可以简化为:

var ords = datastr.splt("").map(byteValue);
相关问题