make apf additions to Array.prototype non enumerable

pull/39/head^2
nightwing 2015-03-09 15:54:24 +04:00
rodzic 9f09732f15
commit d90c3787b7
1 zmienionych plików z 49 dodań i 187 usunięć

Wyświetl plik

@ -2589,13 +2589,22 @@ apf.DATE = 5;
apf.REGEXP = 6;
apf.FUNCTION = 7;
Array.prototype.dataType = apf.ARRAY;
Number.prototype.dataType = apf.NUMBER;
Date.prototype.dataType = apf.DATE;
Boolean.prototype.dataType = apf.BOOLEAN;
String.prototype.dataType = apf.STRING;
RegExp.prototype.dataType = apf.REGEXP;
Function.prototype.dataType = apf.FUNCTION;
function defineProp(obj, name, val) {
Object.defineProperty(obj, name, {
value: val,
enumerable: false,
writable: true,
configurable: true,
});
}
defineProp(Array.prototype, "dataType", apf.ARRAY);
defineProp(Number.prototype, "dataType", apf.NUMBER);
defineProp(Date.prototype, "dataType", apf.DATE);
defineProp(Boolean.prototype, "dataType", apf.BOOLEAN);
defineProp(String.prototype, "dataType", apf.STRING);
defineProp(RegExp.prototype, "dataType", apf.REGEXP);
defineProp(Function.prototype, "dataType", apf.FUNCTION);
/*
@ -2606,10 +2615,10 @@ Function.prototype.dataType = apf.FUNCTION;
* @type Function
* @see apf.extend
*/
Function.prototype.extend = function() {
defineProp(Function.prototype, "extend", function() {
apf.extend.apply(this, [this].concat(Array.prototype.slice.call(arguments)));
return this;
};
});
/*
* Attach a Function object to an event as handler method. If apf.AbstractEvent
@ -2622,7 +2631,7 @@ Function.prototype.extend = function() {
* @type Function
* @see apf.AbstractEvent
*/
Function.prototype.bindWithEvent = function() {
defineProp(Function.prototype, "bindWithEvent", function() {
var __method = this,
args = Array.prototype.slice.call(arguments),
o = args.shift(),
@ -2634,48 +2643,19 @@ Function.prototype.bindWithEvent = function() {
return __method.apply(o, [event].concat(args)
.concat(Array.prototype.slice.call(arguments)));
}
};
/*
* The bind function creates a new function (a bound function) that calls the
* function that is its this value (the bound function's target function) with
* a specified this parameter, which cannot be overridden. bind also accepts
* leading default arguments to provide to the target function when the bound
* function is called. A bound function may also be constructed using the new
* operator: doing so acts as though the target function had instead been
* constructed. The provided this value is ignored, while prepended arguments
* are provided to the emulated function.
*
* @param {Object} context The 'this' context of the bound function
* @type Function
*/
if (!Function.prototype.bind)
Function.prototype.bind = function(context /*, arg1, arg2... */) {
if (typeof this !== 'function') throw new TypeError();
var _arguments = Array.prototype.slice.call(arguments, 1),
_this = this,
_concat = Array.prototype.concat,
_function = function() {
return _this.apply(this instanceof _dummy ? this : context,
_concat.apply(_arguments, arguments));
},
_dummy = function() {};
_dummy.prototype = _this.prototype;
_function.prototype = new _dummy();
return _function;
};
});
/*
* Copy an array, like this statement would: 'this.concat([])', but then do it
* recursively.
*/
Array.prototype.copy = function(){
defineProp(Array.prototype, "copy", function(){
var ar = [];
for (var i = 0, j = this.length; i < j; i++)
ar[i] = this[i] && this[i].copy ? this[i].copy() : this[i];
return ar;
};
});
/*
* Concatenate the current Array instance with one (or more) other Arrays, like
@ -2685,13 +2665,13 @@ Array.prototype.copy = function(){
* @param {Array} array1, array2, array3, etc.
* @type {Array}
*/
Array.prototype.merge = function(){
defineProp(Array.prototype, "merge", function(){
for (var i = 0, k = arguments.length; i < k; i++) {
for (var j = 0, l = arguments[i].length; j < l; j++) {
this.push(arguments[i][j]);
}
}
};
});
/*
* Add the values of one or more arrays to the current instance by using the
@ -2701,7 +2681,7 @@ Array.prototype.merge = function(){
* @type {Array}
* @see Array.copy
*/
Array.prototype.arrayAdd = function(){
defineProp(Array.prototype, "arrayAdd", function(){
var s = this.copy();
for (var i = 0, k = arguments.length; i < k; i++) {
for (var j = 0, l = s.length; j < l; j++) {
@ -2710,7 +2690,7 @@ Array.prototype.arrayAdd = function(){
}
return s;
};
});
/*
* Check if an object is contained within the current Array instance.
@ -2718,12 +2698,12 @@ Array.prototype.arrayAdd = function(){
* @param {Mixed} obj The value to check for inside the Array
* @type {Boolean}
*/
Array.prototype.equals = function(obj) {
defineProp(Array.prototype, "equals", function(obj) {
for (var i = 0, j = this.length; i < j; i++)
if (this[i] != obj[i])
return false;
return true;
};
});
/*
* Make sure that an array instance contains only unique values (NO duplicates).
@ -2821,7 +2801,7 @@ var uniqueBenvie = function(){
}();
if (typeof Set !== "undefined") {
Array.prototype.makeUnique = function(){
defineProp(Array.prototype, "makeUnique", function(){
var out = [],
seen = new Set,
i = this.length;
@ -2834,12 +2814,12 @@ if (typeof Set !== "undefined") {
}
return out;
}
});
}
else {
Array.prototype.makeUnique = function(){
defineProp(Array.prototype, "makeUnique", function(){
return uniqueBenvie(this);
};
});
}
/*
@ -2850,45 +2830,10 @@ else {
* @type {Boolean}
* @see Array.indexOf
*/
Array.prototype.contains = function(obj, from) {
defineProp(Array.prototype, "contains", function(obj, from) {
return this.indexOf(obj, from) != -1;
};
});
/*
* Search for the index of the first occurence of a value 'obj' inside an array
* instance.
* July 29, 2008: added 'from' argument support to indexOf()
*
* @param {Mixed} obj The value to search for inside the array
* @param {Number} [from] Left offset index to start the search from
* @type {Number}
*/
Array.prototype.indexOf = Array.prototype.indexOf || function(obj, from) {
var len = this.length;
for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++) {
if (this[i] === obj)
return i;
}
return -1;
};
/*
* Search for the index of the last occurence of a value 'obj' inside an array
* instance.
*
* @param {Mixed} obj The value to search for inside the array
* @param {Number} [from] Left offset index to start the search from
* @type {Number}
*/
Array.prototype.lastIndexOf = Array.prototype.lastIndexOf || function(obj, from) {
//same as indexOf(), but in reverse loop, JS spec 1.6
var len = this.length;
for (var i = (from >= len) ? len - 1 : (from < 0) ? from + len : len - 1; i >= 0; i--) {
if (this[i] === obj)
return i;
}
return -1;
};
/*
* Like Array.push, but only invoked when the value 'item' is already present
@ -2897,7 +2842,7 @@ Array.prototype.lastIndexOf = Array.prototype.lastIndexOf || function(obj, from)
* @param {Mixed} item, item, ...
* @type {Array}
*/
Array.prototype.pushUnique = function(){
defineProp(Array.prototype, "pushUnique", function(){
var item,
i = 0,
l = arguments.length;
@ -2907,7 +2852,7 @@ Array.prototype.pushUnique = function(){
this.push(item);
}
return this;
};
});
/*
* @todo: Ruben: could you please comment on this function? Seems to serve a very
@ -2915,7 +2860,7 @@ Array.prototype.pushUnique = function(){
*
* I also could not find an occurrence in our codebase.
*/
Array.prototype.search = function(){
defineProp(Array.prototype, "search", function(){
for (var i = 0, length = arguments.length; i < length; i++) {
if (typeof this[i] != "array")
continue;
@ -2926,7 +2871,7 @@ Array.prototype.search = function(){
return this[i];
}
}
};
});
/*
* Iterate through each value of an array instance from left to right (front to
@ -2935,13 +2880,12 @@ Array.prototype.search = function(){
* @param {Function} fn
* @type {Array}
*/
Array.prototype.each =
Array.prototype.forEach = Array.prototype.forEach || function(fn) {
defineProp(Array.prototype, "each", function(fn) {
for (var i = 0, l = this.length; i < l; i++)
if (fn.call(this, this[i], i, this) === false)
break;
return this;
}
});
/*
* Search for a value 'obj' inside an array instance and remove it when found.
@ -2949,7 +2893,7 @@ Array.prototype.forEach = Array.prototype.forEach || function(fn) {
* @type {Mixed} obj
* @type {Array}
*/
Array.prototype.remove = function(obj) {
defineProp(Array.prototype, "remove", function(obj) {
for (var i = this.length - 1; i >= 0; i--) {
if (this[i] != obj)
continue;
@ -2958,7 +2902,7 @@ Array.prototype.remove = function(obj) {
}
return this;
};
});
/*
* Remove an item from an array instance which can be identified with key 'i'
@ -2966,10 +2910,10 @@ Array.prototype.remove = function(obj) {
* @param {Number} i
* @return {Mixed} The removed item
*/
Array.prototype.removeIndex = function(i) {
defineProp(Array.prototype, "removeIndex", function(i) {
if (!this.length) return null;
return this.splice(i, 1);
};
});
/*
* Insert a new value at a specific object; alias for Array.splice.
@ -2978,9 +2922,9 @@ Array.prototype.removeIndex = function(i) {
* @param {Number} i Index to insert 'obj' at
* @type {Number}
*/
Array.prototype.insertIndex = function(obj, i) {
defineProp(Array.prototype, "insertIndex", function(obj, i) {
this.splice(i, 0, obj);
};
});
/*
* Reverses the order of the elements of an array; the first becomes the last,
@ -2988,89 +2932,7 @@ Array.prototype.insertIndex = function(obj, i) {
*
* @type {Array}
*/
Array.prototype.invert =
Array.prototype.reverse = Array.prototype.reverse || function(){
var l = this.length - 1;
for (var temp, i = 0; i < Math.ceil(0.5 * l); i++) {
temp = this[i];
this[i] = this[l - i]
this[l - i] = temp;
}
return this;
};
/*
* Attempt to fully comply (in terms of functionality) with the JS specification,
* up 'till version 1.7:
* @link http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array
*/
/*
* Creates a new array with all of the elements of this array for which the
* provided filtering function returns true.
*
* @param {Function} fn Function to test each element of the array.
* @param {Object} bind Object to use as this when executing callback.
* @type {Array}
*/
Array.prototype.filter = Array.prototype.filter || function(fn, bind) {
var results = [];
for (var i = 0, l = this.length; i < l; i++) {
if (fn.call(bind, this[i], i, this))
results.push(this[i]);
}
return results;
};
/*
* Returns true if every element in this array satisfies the provided testing
* function.
*
* @param {Function} fn Function to test for each element.
* @param {Object} bind Object to use as this when executing callback.
* @type {Boolean}
*/
Array.prototype.every = Array.prototype.every || function(fn, bind) {
for (var i = 0, l = this.length; i < l; i++) {
if (!fn.call(bind, this[i], i, this))
return false;
}
return true;
};
/*
* Creates a new array with the results of calling a provided function on every
* element in this array.
*
* @param {Function} fn Function that produces an element of the new Array from an element of the current one.
* @param {Object} bind Object to use as this when executing callback.
* @type {Array}
*/
Array.prototype.map = Array.prototype.map || function(fn, bind) {
var results = [];
for (var i = 0, l = this.length; i < l; i++)
results[i] = fn.call(bind, this[i], i, this);
return results;
};
/*
* Tests whether some element in the array passes the test implemented by the
* provided function.
*
* @param {Function} fn Function to test for each element.
* @param {Object} bind Object to use as this when executing callback.
* @type {Boolean}
*/
Array.prototype.some = Array.prototype.some || function(fn, bind) {
for (var i = 0, l = this.length; i < l; i++) {
if (fn.call(bind, this[i], i, this))
return true;
}
return false;
};
defineProp(Array.prototype, "invert", Array.prototype.reverse);
@ -22453,8 +22315,8 @@ apf.runNonIe = function (){
//Document.prototype.onreadystatechange = null;
Document.prototype.parseError = 0;
Array.prototype.item = function(i){return this[i];};
Array.prototype.expr = "";
defineProp(Array.prototype, "item", function(i){return this[i];});
defineProp(Array.prototype, "expr", "");
/*try{
XMLDocument.prototype.readyState = 0;