From b7814e5e24a69afab3a87a530f41b918a6fe6284 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 6 Jan 2015 19:58:19 +1100 Subject: [PATCH] add documentation on optional protoToAssign transformer --- doc/caveats.md | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/doc/caveats.md b/doc/caveats.md index 9c56bd744f..42e5fdd925 100644 --- a/doc/caveats.md +++ b/doc/caveats.md @@ -44,20 +44,39 @@ via [\_\_proto\_\_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe this is widely supported but you may run into problems with much older browsers. **NOTE:** `__proto__` is not supported on IE <= 10 so static properties -**will not** be inherited. A possible workaround is to use `super();`: +**will not** be inherited. -```js -class Foo { - static foo() { +You can use the `protoToAssign` optional transformer that will transform all +`__proto__` assignments to a method that will shallowly copy it over all +properties. - } -} +```javascript +require("6to5").transform("code", { optional: ["protoToAssign"] }); +``` -class Bar extends Foo { - static foo() { - super(); - } -} +```sh +$ 6to5 --optional protoToAssign script.js +``` + +This means that the following **will** work: + +```javascript +var foo = { a: 1 }; +var bar = { b: 2 }; +bar.__proto__ = foo; +bar.a; // 1 +bar.b; // 2 +``` + +however the following **will not**: + +```javascript +var foo = { a: 1 }; +var bar = { b: 2 }; +bar.__proto__ = foo; +bar.a; // 1 +foo.a = 2; +bar.a; // 1 - should be 2 but remember that nothing is bound and it's a straight copy ``` ### Getters/setters (8 and below)