Skip to content

Instantly share code, notes, and snippets.

@Hyvi
Created November 8, 2011 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hyvi/1347493 to your computer and use it in GitHub Desktop.
Save Hyvi/1347493 to your computer and use it in GitHub Desktop.
array.map 和 sendAsBinary的兼容实现
/**
* http://javascript0.org/wiki/Portable_sendAsBinary
* portable sendAsBinary
*/
XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
function byteValue(x) {
return x.charCodeAt(0) & 0xff;
}
var ords = Array.prototype.map.call(datastr, byteValue);
var ui8a = new Uint8Array(ords);
this.send(ui8a.buffer);
}
/**
* http://www.tutorialspoint.com/javascript/array_map.htm
* Array.map(callback[,thisObj])
* This method is a JavaScript extension to the ECMA-262 standard
* Compatibility
*/
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment