Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Last active August 29, 2015 14:07
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 Swivelgames/8acd6368cb720ddbaae2 to your computer and use it in GitHub Desktop.
Save Swivelgames/8acd6368cb720ddbaae2 to your computer and use it in GitHub Desktop.
Executable Array (Executable Array of Callbacks)
/**
* @class ExecutableArray
* @extends Array.prototype
* @param {Array} arr Original Array (if any)
* @author Joseph Dalrymple <me@swivel.in>
* @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