A case for removing the seal/freeze/isSealed/isFrozen traps

Claus Reinke claus.reinke at talk21.com
Mon Feb 18 13:46:16 PST 2013


> as a "high-integrity" function:
> 
>     var freeze = Object.freeze,
>         push = Function.prototype.call.bind(Array.prototype.push);
>     function makeTable() {
>       var array = [];
>       return freeze({
>         add: function(v) { push(array, v); },
>         store: function(i, v) { array[i >>> 0] = v; },
>         get: function(i) { return array[i >>> 0]; }
>       });
>     }

Careful there, you're not done!-) With nodejs, adding the following

    var table = makeTable();
    table.add(1);
    table.add(2);
    table.add(3);

    var secret;
    Object.defineProperty(Array.prototype,42,{get:function(){ secret = this;}});

    table.get(42);
    console.log(secret);
    secret[5] = "me, too!";

    console.log( table.get(5) );

to your code prints

    $ node integrity.js
    [ 1, 2, 3 ]
    me, too!

Couldn't resist,
Claus
 


More information about the es-discuss mailing list