es6快速入门(2)

1.字符串增强

indexOf类型增强

'my string'.startsWith('my'); //true
'my string'.endsWith('my');   //false
'my string'.includes('str');  //true
//es5写法
var str = 'my string';
str.indexOf('my') == 0; //true
str.lastIndexOf('my') == (str.length - 'my'.length); // false
str.indexOf('str') > -1; // true

重复字符串

我感觉在写python

'my '.repeat(3); // 'my my my '

模版字符串

模板字符串提供了一个简洁的方式去创建字符串和实现字符串插值。见代码:

let name = 'John';
let apples = 5;
let pears = 7;
let bananas = () => 3; 

console.log(`This is ${name}.`);
console.log(`He carries ${apples} apples, ${pears} pears, and ${bananas()} bananas.`);

// ES5 equivalent:
console.log('He carries ' + apples + ' apples, ' + pears + ' pears, and ' + bananas() +' bananas.');
//ES5
var data ='hid='+hid+'&name='+that.name+'&email='+that.email+'&contact='+that.contact+'&comment='+that.comment;
$.get('/_api/House/houseQuery?'+data,function(d){});

//ES6
let data = 'hid=${hid}&name=${self.name}&email=${self.email}&contact=${self.contac}&comment=${self.comment}';
$.get('/_api/House/houseQuery?${data}',function(d){});

ES6 Overview in 350 Bullet Points

ES6 Overview in 350 Bullet Points

你可能感兴趣的:(es6快速入门(2))