Minimalist (why) classes ?

Rick Waldron waldron.rick at gmail.com
Mon Nov 14 10:42:49 PST 2011


On Mon, Nov 14, 2011 at 11:48 AM, John J Barton <johnjbarton at johnjbarton.com
> wrote:

> [snip]
>
> Sorry I don't understand. Every function which accepts object
> references and embeds its arguments in [[Prototype]] (either in the
> return value or the instance created from the return value)  faces the
> copy-ish problem.


This is a negligent conflation. Pass by reference is not the same as
ensuring that when extending one object with another, the resulting object
is not implicitly capable of mutating its source objects (as is the case in
Selfish). In Prototype.js's Object.extend(), the behaviour is even worse:

// Object.extend from prototype.js
function extend(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
}


var defaults = {
  config: {
    a: "alpha",
    b: "beta"
  }
};


function F( options ) {
  this.settings = extend( defaults, options );
}

var f = new F({
          config: {
            a: "DESTROYED"
          }
        });

console.log( defaults.config.a ); // "DESTROYED"


Whoops! Now my `defaults` have been changed. If I had other functions
relying on that object, everything becomes chaos. It's important to
remember that Prototype.js has very little developer mindshare these days
and should absolutely not be used as source material for spec reification.

The tests I wrote for Selfish illustrate that it also has this behaviour,
but is more deceiving because you think you're getting new "instances".

Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111114/0d6faeb1/attachment.html>


More information about the es-discuss mailing list