JSFiddle - React, Tailwind, and code Playground
by khanfused
JavaScript
(function(window, undefined) {
window.Stack = function(stack) {
if (!(this instanceof Stack)) {
throw new TypeError('Stack must be called with the `new` keyword.');
} else {
if (stack instanceof Array) {
this.stack = stack;
} else {
Array.prototype.push.apply(this.stack = [], Array.prototype.slice.call(arguments));
}
Array.prototype.push.apply(this, this.stack);
this.length = this.stack.length;
}
};
Stack.prototype = {
events: {
},
on: function(event, callback) {
this.events[event].push(callback);
return this;
},
trigger: function(event, args) {
this.events[event].forEach(function(callback) {
callback.call(this.stack, args);
});
return this;
}
};
'fill pop push reverse shift sort splice unshift concat includes join slice indexOf lastIndexOf forEach every some filter find findIndex reduce'.split(' ').forEach(function(method) {
Stack.prototype.events[method] = [];
Stack.prototype[method] = function() {
return this.trigger(method, this.stack[method].apply(this.stack, this.stack.slice.call(arguments)));
}
});
}(window));