JavaScript Queue and callbacks
Blogs20172017-01-12
JavaScript Queue and callbacks
Write some quick and random codes trying to improve JavaScript Queue for promise calling. Keep them here for quick retrieving:
//1. callback, or action, or publisher
const modifyText = (e) => {
console.info('hello world', e.detail);
}
//2. add an anchor: div id="eventNode"
var el = document.getElementById("eventNode");
el.addEventListener("clickMe", modifyText, false);
//3. basic custom event trigger
var event = new Event('clickMe');
el.dispatchEvent(event);
// trigger with parameter
event = new CustomEvent('clickMe', { 'detail': +new Date })
el.dispatchEvent(event)
// integrate above with a global Queue?
var queue = [];
const cb1 = () => console.log('cb1');
const cb2 = param => console.log('cb2', param);
const cb3 = arg => console.log('cb3', arg);
queue.push(cb1);
queue.push(cb2);
queue.push(cb3);
function Shape() {
this.x = 0; this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x; this.y += y;
console.info('Shape moved.');
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
queue.push(rect);
queue.forEach(function(q) {
if(q instanceof Rectangle) {
q.move(1, 1);
}
else {
q.call(null, (+new Date));
}
})It works. How does googletag.pubads().cmd queue manage?
