|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +title: ES6 Features |
| 4 | +--- |
| 5 | + |
| 6 | +React can optionally use some ECMAScript 6 features thanks to the bundled version of [JSTransform](https://github.com/facebook/jstransform). ECMAScript 6 (or "ES6" for short) is the next version of ECMAScript/JavaScript and contains several useful features: |
| 7 | + |
| 8 | +* **[Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/arrow_functions)** — A syntax for inline lambda functions similar to C#. These are very useful when combined with the `map` and `filter` methods of arrays: |
| 9 | + |
| 10 | +```javascript |
| 11 | +var numbers = [1, 2, 3, 4]; |
| 12 | +var doubled = numbers.map(number => number * 2); // [2, 4, 6, 8] |
| 13 | +``` |
| 14 | + |
| 15 | +Arrow functions also implicitly bind `this`, so you do not need to write `.bind(this)` when passing around a function as a callback. |
| 16 | + |
| 17 | +* **Concise methods** — You no longer need to write `: function` in your object literals: |
| 18 | + |
| 19 | +```javascript{13,16} |
| 20 | +// The old way |
| 21 | +var OldAndBusted = React.createClass({ |
| 22 | + render: function() { |
| 23 | + // ... |
| 24 | + }, |
| 25 | + doStuff: function() { |
| 26 | + // ... |
| 27 | + } |
| 28 | +}); |
| 29 | +
|
| 30 | +// The new way |
| 31 | +var NewHotness = React.createClass({ |
| 32 | + render() { |
| 33 | + // ... |
| 34 | + }, |
| 35 | + doStuff() { |
| 36 | + // ... |
| 37 | + } |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +* **[Classes](http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes)** — Similar to the class systems included in JavaScript frameworks such as Prototype and MooTools, but will (eventually) be native to JavaScript |
| 42 | + |
| 43 | +```javascript |
| 44 | +class AwesomeStuff { |
| 45 | + add(first, second) { |
| 46 | + return first + second; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +var foo = new AwesomeStuff(); |
| 51 | +foo.add(2, 3); // 5 |
| 52 | +``` |
| 53 | + |
| 54 | +* **[Short object notation](http://ariya.ofilabs.com/2013/02/es6-and-object-literal-property-value-shorthand.html)** |
| 55 | +* And more! See the [JSTransform source code](https://github.com/facebook/jstransform/tree/master/visitors), you never know what goodies you'll find. |
| 56 | + |
| 57 | +How to use |
| 58 | +---------- |
| 59 | +To use the ES6 transforms, you'll need to enable them. For ASP.NET MVC sites, this is done in your `ReactConfig.cs` by calling `.SetUseHarmony(true)`: |
| 60 | + |
| 61 | +```csharp{2} |
| 62 | +ReactSiteConfiguration.Configuration |
| 63 | + .SetUseHarmony(true) |
| 64 | + .AddScript("~/Content/Sample.jsx"); |
| 65 | +``` |
| 66 | +If you are using [MSBuild to precompile your JSX](/guide/msbuild.html), you also need to enable it in MSBuild via the `UseHarmony="true"` flag in your build script (`TransformJsx.proj` by default): |
| 67 | + |
| 68 | +```xml |
| 69 | +<TransformJsx SourceDir="$(MSBuildProjectDirectory)" UseHarmony="true" /> |
| 70 | +``` |
0 commit comments