javascript

SamePlace spinoffs #1: CSS Query

Before:


var xulScriptlet;
var blueprint = document.getElementById('blueprints').firstChild;
while(blueprint) {
    if(blueprint.getAttribute('class') == 'scriptlet')
        break;
    blueprint = blueprint.nextSibling;
}

var xulScriptlet = blueprint.cloneNode(true);
xulScriptlet.getElementsByAttribute('class', 'name')[0].value = 
    scriptlet.info.name;
xulScriptlet.getElementsByAttribute('class', 'version')[0].value = 
    scriptlet.info.version;
document.getElementById('scriptlets').appendChild(xulScriptlet);

After...

Memoize in JavaScript

In JavaScript, functions are objects. No big deal, until you expand that to functions can have state, and realize that things like this become possible:


function fact(n) {
    var memo = arguments.callee.memo;
    if(!(n in memo))
        if(n == 0)
            memo[n] = 1;
        else
            memo[n] = n * fact(n - 1);

    return memo[n];
}
fact.memo = {};

(arguments.callee is how you reference a function from within the function itself.)

Syndicate content