var foo = function(){return 1+1;}; //等价于 let foo = () => 1+1;
var foo = function(a, b){ if (a !== undefined && b !== undefined) { return a + b } else { return 0 } }; //等价于 var add = (a, b) => { if (a !== undefined && b !== undefined) { return a + b } else { return 0 } }
let arr = [ 'foo', 'bar', 'baz' ]; for( let element of arr ){ console.log( element ); } // foo // bar // baz for( let [ index, element ] of arr.entries() ){ console.log( index + "." + element ); } // foo // bar // baz
模块化
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// lib.js export const sqet = Math.sqrt; export function square( x ){ return x * x ; } export function diag( x, y ){ return sqrt( square(x) + square( y ) ); }
let str = String.raw`This is a text with multiple lines. Escapes are not interpreted, \n is not a newline.`
var parts = '/2012/10/Page.html'.match(XRegExp.rx` ^ # match at start of string only / (?<year> [^/]+ ) # capture top dir name as year / (?<month> [^/]+ ) # capture subdir name as month / (?<title> [^/]+ ) # capture base name as title \.html? $ # .htm or .html file ext at end of path `); console.log( parts.year ); // 2012