Hi,

First and foremost, thanks for the notes :-)

Le 28/07/2012 01:55, Rick Waldron a écrit :
> Fix "override mistake"
>
> # The can put check
> (...)
>
> Property in a prototype object that is read-only cannot be shadowed.
>
> Just the same as get-only accessor.
I'd like to add a use case here. Every once in a while, I write
something like:

    var a = [];
    a.push = function(elem){
        if(condition(elem)){
            // do something like change the elem value then do an actual
push
            // or throw an error
            // or just ignore this value to avoid duplicates, for instance
        }
        else{
            Array.prototype.push.call(this, elem)
        }
    };

    // use a.push (there is an implicit contract on only using .push to
add elements)

There is such a snippet in a Node.js server in production right now, so
that's really not hypothetical code. If I ever consider to move to SES,
then, before the above snippet is run, Array.prototype gets frozen and
the "a.push" assignment will fail (at runtime!).


Several things here:
* I could change a.__proto__, but it's a bit weird since the condition
in the custom push is often very specific to this exact array, so
changing the [[prototype]] feels like too much, just for one instance
(though that would work fine)
* I could use Object.defineProperty, but the above code is definitely
more readable and intuitive.
* An implicit contract is not the best idea ever, but that works when
the array. In an ES6 world the array would certainly be a proxy and
whatever invariant could be preserved even for numerical property value
assignments. But we're not there yet, so that's not an option

As far as I'm concerned, the biggest issue with this use case is that I
have written code which reads well (I'm open to debate on that if some
disagree) and that what is read may not be what will occur.
Also, if one day, one Node.js module I use decides it's better to freeze
Array.prototype, it will be a very painful bug to track down when I
update. It would be mush easier to track down if I was monkey-patching
Array.prototype.push, but I'm not.

As a final note, I don't know how often people do what I've describe.
I'll adapt my code if what is decided is to keep the [[canPut]] error,
but I don't know how many people this kind of problem would affect.

David
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to