Make Utils.js use Object.define to modify Array
Previously, we were modifying Array's prototype using simple assignment. This can mess with enumeration/iteration. Thus, we now use Object.defineProperty with enumerable set to false. See #366
This commit is contained in:
parent
58529d347b
commit
f9fd0313b8
|
@ -17,30 +17,36 @@ var Util = {};
|
||||||
* Make arrays quack
|
* Make arrays quack
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Array.prototype.push8 = function (num) {
|
var addFunc = function (cl, name, func) {
|
||||||
"use strict";
|
if (!cl.prototype[name]) {
|
||||||
this.push(num & 0xFF);
|
Object.defineProperty(cl.prototype, name, { enumerable: false, value: func });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Array.prototype.push16 = function (num) {
|
addFunc(Array, 'push8', function (num) {
|
||||||
|
"use strict";
|
||||||
|
this.push(num & 0xFF);
|
||||||
|
});
|
||||||
|
|
||||||
|
addFunc(Array, 'push16', function (num) {
|
||||||
"use strict";
|
"use strict";
|
||||||
this.push((num >> 8) & 0xFF,
|
this.push((num >> 8) & 0xFF,
|
||||||
num & 0xFF);
|
num & 0xFF);
|
||||||
};
|
});
|
||||||
Array.prototype.push32 = function (num) {
|
|
||||||
|
addFunc(Array, 'push32', function (num) {
|
||||||
"use strict";
|
"use strict";
|
||||||
this.push((num >> 24) & 0xFF,
|
this.push((num >> 24) & 0xFF,
|
||||||
(num >> 16) & 0xFF,
|
(num >> 16) & 0xFF,
|
||||||
(num >> 8) & 0xFF,
|
(num >> 8) & 0xFF,
|
||||||
num & 0xFF);
|
num & 0xFF);
|
||||||
};
|
});
|
||||||
|
|
||||||
// IE does not support map (even in IE9)
|
// IE does not support map (even in IE9)
|
||||||
//This prototype is provided by the Mozilla foundation and
|
//This prototype is provided by the Mozilla foundation and
|
||||||
//is distributed under the MIT license.
|
//is distributed under the MIT license.
|
||||||
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
|
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
|
||||||
if (!Array.prototype.map) {
|
addFunc(Array, 'map', function (fun /*, thisp*/) {
|
||||||
Array.prototype.map = function (fun /*, thisp*/) {
|
|
||||||
"use strict";
|
"use strict";
|
||||||
var len = this.length;
|
var len = this.length;
|
||||||
if (typeof fun != "function") {
|
if (typeof fun != "function") {
|
||||||
|
@ -56,15 +62,13 @@ if (!Array.prototype.map) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
};
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// IE <9 does not support indexOf
|
// IE <9 does not support indexOf
|
||||||
//This prototype is provided by the Mozilla foundation and
|
//This prototype is provided by the Mozilla foundation and
|
||||||
//is distributed under the MIT license.
|
//is distributed under the MIT license.
|
||||||
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
|
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
|
||||||
if (!Array.prototype.indexOf) {
|
addFunc(Array, 'indexOf', function (elt /*, from*/) {
|
||||||
Array.prototype.indexOf = function (elt /*, from*/) {
|
|
||||||
"use strict";
|
"use strict";
|
||||||
var len = this.length >>> 0;
|
var len = this.length >>> 0;
|
||||||
|
|
||||||
|
@ -81,8 +85,7 @@ if (!Array.prototype.indexOf) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
};
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
|
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
|
||||||
if (!Object.keys) {
|
if (!Object.keys) {
|
||||||
|
@ -131,8 +134,7 @@ if (!Object.keys) {
|
||||||
//This prototype is provided by the Mozilla foundation and
|
//This prototype is provided by the Mozilla foundation and
|
||||||
//is distributed under the MIT license.
|
//is distributed under the MIT license.
|
||||||
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
|
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
|
||||||
if (!Function.prototype.bind) {
|
addFunc(Function, 'bind', function (oThis) {
|
||||||
Function.prototype.bind = function (oThis) {
|
|
||||||
if (typeof this !== "function") {
|
if (typeof this !== "function") {
|
||||||
// closest thing possible to the ECMAScript 5
|
// closest thing possible to the ECMAScript 5
|
||||||
// internal IsCallable function
|
// internal IsCallable function
|
||||||
|
@ -153,8 +155,7 @@ if (!Function.prototype.bind) {
|
||||||
fBound.prototype = new fNOP();
|
fBound.prototype = new fNOP();
|
||||||
|
|
||||||
return fBound;
|
return fBound;
|
||||||
};
|
});
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// requestAnimationFrame shim with setTimeout fallback
|
// requestAnimationFrame shim with setTimeout fallback
|
||||||
|
|
Loading…
Reference in New Issue