a/20299788/1636522
by wared
CSS
*{font-family:Consolas}
.line{padding:2px 0;border-bottom:1px solid #ccc}
JavaScript
// Letters
var Letters = function(word) {
Array.prototype.push.apply(this, word.split(''));
};
Letters.prototype.each = function (fn) {
var i = 0, l = this.length;
for(; i < l; i++) {
fn.call(this, this[i], i);
}
return this;
};
Letters.prototype.addLetter = function(toAdd) {
this.each(function (item, i) {
this[i] += toAdd;
});
return this;
};
Letters.prototype.valueOf = function() {
return Array.prototype.join.call(this);
};
// playground
var l1 = new Letters('test');
var l2 = new Letters('demo');
l1.each(function (item, i) {
output(i + ' ' + item);
this[i] = item += 'x'; // adds a letter
output(i + ' ' + item);
}).each(function (item, i) {
this[i] = item.toUpperCase();
});
output(l1);
output(l2.addLetter('x'));
// helper
function output(s) {
document.body.innerHTML += '<div class="line">' + s + '</div>';
}