15
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Modules in ECMAScript 6.0

Embed Size (px)

Citation preview

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

export const sqrt = Math.sqrt;export function square(x) { return x * x; }export function diag(x, y) {

return sqrt(square(x) + square(y));}

import { square, diag } /* or '*' */ from 'lib';

console.log(square(11)); // 121console.log(diag(4, 3)); // 5

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

//------ myFunc.js ------export default function () { ... };

//------ main1.js ------import myFunc from 'myFunc';myFunc();

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

if (Math.random()) {

exports.baz = ...;

}

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

// Export lib

export * from 'src/lib';

// Export from lib just foo and bar

export { foo, bar } from 'src/lib';

// Export from lib, foo as myFoo

export { foo as myFoo, bar } from 'src/other_module';

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

// Default exports and named exportsimport theDefault from 'src/mylib';import { named1, named2 } from 'src/mylib';import theDefault, { named1, named2 } from 'src/mylib';

// Renaming: import named1 as myNamed1import { named1 as myNamed1, named2 } from 'src/mylib';

// Importing the module as an objectimport * as mylib from 'src/mylib';

// Only load the moduleimport 'src/mylib';

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

import { url } from this module;

console.log(url);

// Option II

import * as metaData from this module;

console.log(metaData.url);

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Promise.all(

['module1', 'module2', 'module3']

.map(x => System.import(x)))

.then(function ([module1, module2, module3]) { ... });

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

http://www.2ality.com/

Understanding ECMAScript 6

http://ecmascript6.org/

A Few New Things Coming To JavaScript

HARMONY OF DREAMS COME TRUE

Harmony specification_drafts

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

eyalvardi.wordpress.com