Last active
August 29, 2015 14:07
-
-
Save Swivelgames/8acd6368cb720ddbaae2 to your computer and use it in GitHub Desktop.
Executable Array (Executable Array of Callbacks)
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
/** | |
* @class ExecutableArray | |
* @extends Array.prototype | |
* @param {Array} arr Original Array (if any) | |
* @author Joseph Dalrymple <[email protected]> | |
* @description Implements `call` and `apply` on an Array-like object | |
*/ | |
var ExtNodeList = (function(){ | |
/** | |
* Constructor Function | |
* @constructor | |
* @description Adds elements from supplied Array to self | |
*/ | |
var Constructor = function(arr){ | |
if (!arr || arr.length < 1) return; | |
// If Array provided, set | |
for(var i=0;i<this.length;i++) this.push(arr[i]); | |
}; | |
/** | |
* Methods and Properties | |
*/ | |
Constructor.prototype = Object.create( | |
// Implement the Array Prototype | |
Array.prototype || Object.getPrototypeOf(Array.prototype), | |
// Enhance existing methods | |
{ | |
/** | |
* @function | |
* @name call | |
* @description Execute all callbacks using `call` | |
* @returns {Array} Return values | |
*/ | |
call: { | |
writable: false, | |
configurable: false, | |
enumerable: false, | |
value: function() { | |
var ret = []; | |
// Iterate over contained nodes | |
for(var i=0;i<this.length;i++) { | |
var elem = this[i]; | |
ret.push( | |
Function.prototype.call.apply(elem, arguments) | |
); | |
} | |
// Return resulting ExtNodeList | |
return ret; | |
} | |
}, | |
/** | |
* @function | |
* @name apply | |
* @description Execute all callbacks using `apply` | |
* @returns {Array} Return values | |
*/ | |
apply: { | |
writable: false, | |
configurable: false, | |
enumerable: false, | |
value: function() { | |
var ret = [], | |
args = Array.prototype.slice.apply(arguments), | |
context = Array.prototype.splice.apply(args,[0,1]); | |
// Iterate over contained nodes | |
for(var i=0;i<this.length;i++) { | |
var elem = this[i]; | |
ret.push( | |
Function.prototype.apply.apply(elem,[context, args]) | |
); | |
} | |
// Return resulting ExtNodeList | |
return ret; | |
} | |
} | |
} | |
); | |
// Set declared Constructor to ExtNodeList var | |
return Constructor; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment