Hi,

Le 23/02/2015 10:10, Michał Wadas a écrit :
Cloning objects is long requested feature.
"clone object javascript" yields 1 480 000 results in Google.
I'd like to share this as an answer
http://facebook.github.io/immutable-js/#the-case-for-immutability
"If an object is immutable, it can be "copied" simply by making another reference to it instead of copying the entire object. Because a reference is much smaller than the object itself, this results in memory savings and a potential boost in execution speed for programs which rely on copies (such as an undo-stack)."

```js
var map1 = Immutable.Map({a:1, b:2, c:3});
var clone = map1;
```

Despite people *saying* all over the Internet they want cloning, maybe they want immutability?

My proposition is to create a new well known Symbol - Symbol.clone and
corresponding method on Object - Object.clone.

Default behavior for an object is to throw on clone try.
Object.prototype[Symbol.clone] = () => { throw TypeError; }
Users are encorauged to define their own Symbol.clone logic.

Primitives are cloned easily.
Number.prototype[Symbol.clone] = String.prototype[Symbol.clone] =
Boolean.prototype[Symbol.clone] = function() {return this.valueOf();}
Primitives are immutable, no need to clone them.
If you're referring to "primitive objects", it might be better to forget about this weird corner of the language than polish it.

Back to something you wrote above:
Users are encorauged to define their own Symbol.clone logic.
Perhaps this cloning protocol can be purely implemented in userland as a library and doesn't need support from the language. That's one of the reasons symbols have been introduced after all.

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

Reply via email to