Java Reference
In-Depth Information
and mean() functions that we wrote in Chapter 11 , we could declare these functions as
named exports:
export function sum(array, callback) {
if(typeof callback === "function") {
array = array.map(callback);
}
return array.reduce( function(a,b) { return a + b });
}
export function mean(array) {
return sum(array)/array.length;
}
To then import this in the main scripts.js file, you'd add this line of code:
import { sum, mean } from averages;
Now the sum() and mean() functions can be used in the scripts.js file.
Everything in a module file can be imported using this notation:
import * as averages from 'averages';
This will then import all the functions from the averages.js file and they'll be given a
namespace of averages . So, the mean function could be used as follows:
averages.mean([2,6,10]);
Ready to Use Today
Certain browsers have already started implementing the new features in Harmony, although
it might be some time before they're all fully supported. It is possible to implement
these features now using a transpiler , which converts code written in ECMAScript 6 into
JavaScript code to work in modern browsers. One example of a transpiler is the Google
project Traceur, which supports many features of ECMAScript 6 and generates code that
runs reasonably well on modern browsers. If you're using Node.js to run any JavaScript, it
is possible to add the --harmony flag to get support for most of the new features.
Search WWH ::




Custom Search