A case for removing the seal/freeze/isSealed/isFrozen traps
Nathan Wall
nathan.wall at live.com
Mon Feb 18 14:27:50 PST 2013
Claus Reinke wrote:
> 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
>
Nice! This is not something I had considered. Aside from freezing Array.prototype, I can only really think of one solution: not use an array.
var create = Object.create,
freeze = Object.freeze,
push = Function.prototype.call.bind(Array.prototype.push);
function makeTable() {
var array = create(null);
return freeze({
add: function(v) { push(array, v); },
store: function(i, v) { array[i >>> 0] = v; },
get: function(i) { return array[i >>> 0]; }
});
}
I suppose the array isn't really needed since we're not using methods inherited from Array.prototype. Downside: Browsers won't know to optimize the object as an array.
(Side note: Mark's original post was in the context of frozen Array.prototype on non-compliant implementations which allow writing to inherited non-writable properties. I find this a fun exercise without frozen Array.prototype, though.)
Nathan
More information about the es-discuss
mailing list