Created
November 8, 2011 10:57
-
-
Save Hyvi/1347493 to your computer and use it in GitHub Desktop.
array.map 和 sendAsBinary的兼容实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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