class Entries{ // ... selectEntries( { from=0, to=this.length } = {} ){ // Long: { from: from=0, to: to=this.length } // Use 'from' and 'to' } }
let entries = new Entriea(); entries.selectEntries( { from:5, to: 15 } ); entries.selectEntries( { from:5 } ); entries.selectEntries( { to: 15 } ); entries.selectEntries( { } ); entries.selectEntries();
1 2 3
`模板字符串:`
1 2 3 4 5 6 7 8 9 10 11 12 13
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
class Person { constructor( nane ){ this.name = name; } describe(){ return "Person called" + this.name; } } // 子类 class Employee extends Person{ constructor ( name, title ){ // super.constructor( name ) super( name ); this.title = title; } describe(){ // super.describe() return super() + "(" + this.title + ")"; } }
1 2 3
`内置函数的子类,如 Error 和 Array:`
1 2 3
class MyError extends Error{ // ... }
1 2 3
`for-of 循环(对于所有遵守 ES6 迭代规则的对象均有效)`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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
`模块化`
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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 ) ); }
var domList = document.querySelectorAll( 'a[href]' ); var links = Array.prototype.slice.call( domList ); links.forEach(function( link ){ console.log( link ); });
也可以将 arguments 转化为数组:
1 2 3 4 5
function format(pattern) { var params = Array.prototype.slice.call(arguments, 1); return params; } console.log(format('a', 'b', 'c')); // ['b', 'c']
var fs = require('fs'); var rs = fs.createReadStream('chinese.md'); rs.on("data", function (chunk){ console.log( Buffer.isBuffer( chunk ) ) });
从上面可知其均为 Buffer 类型片段,那么在一些情况下也会使得我们遇到一些隐藏的问题:
1 2 3 4 5 6 7 8 9
var fs = require('fs'); var rs = fs.createReadStream('chinese.md' , { highWaterMark: 5 } ); var data = ''; rs.on("data", function (chunk){ data += chunk; }); rs.on("end", function () { console.log(data); });
var fs = require('fs'); var rs = fs.createReadStream('chinese.md', { highWaterMark: 5 } ); var dataArr = [], len = 0, data; rs.on("data", function (chunk){ dataArr.push(chunk); len += chunk.length; }); rs.on("end", function () { data = Buffer.concat( dataArr, len ).toString(); console.log(data); });
var buf = new Buffer('this is text concat test'), str = 'this is text concat test'; console.time('buffer concat test'); var list = []; var len= 100000 * buf.length; for(var i=0;i<100000;i++){ list.push(buf); len += buf.length; } var s1 = Buffer.concat(list, len).toString(); console.timeEnd('buffer concat test'); console.time('string concat test'); var list = []; for (var i = 100000; i >= 0; i--) { list.push(str); } var s2 = list.join(''); console.timeEnd('string concat test');