1.函数扩展
ES6标准感觉越来越向传统语言靠拢了,以后写到ES6的标准都记录下:
var array = [,,]; // =>操作符
array.forEach(v => console.log(v));
是不是简化了回调函数的写法。
=>可以简化函数的写法
var single = a => a; //single为函数;参数为a,函数体就只有a;
console.log(single('hello, world'));
如果没有参数;那么写法如下:
var log = () => {
alert('no param');
}
注意点:
1.1.typeof运行符和普通的function一样
var func = a =>a;
console.log(typeof func); //function
1.2.this固定,不在善变
var obj= {
data:['John Backus','John Hopcroft'],
init:function(){
document.onclick = ev => {
alert(this.data);
}
}
}
obj.init(); //John Backus,John Hopcroft
1.3.箭头函数不能用new
var Person = (name,age)=>{
this.name= name;
this.age = age;
}
var p =new Person('John',33);
//Uncaught TypeError: (name,age)=>{(…)
1.4.不能使用argument
var func = ()= >{
console.log(arguments);
} //出错
//重要的莫过于class关键字;它提供类的创建,是JS语言的OOP编程更像JAVA\C++等语言。测试了下,居然不支持
class Animal {
//ES6的新型构造器
constructor(name){
this.name =name
}
sayName(){
console.log('My name is' + this.name);
}
}
class Programmer extends Animal {
constructor(name){
super(name);
}
program(){
console.log("I'm coding");
}
}
var animal = new Animal('dummy'),
wayou = new Programmer('wayou');
animal.sayName();
wayou.sayName();
wayou.program();
2.Object扩展
Object.is // 等价于"===";
Object.assgin(targetObj,sourceObj1,souceObj2......);
var x = Object.assign({}, undefined, {
sourceMap: 'xy'
});
console.log(x);
// {sourceMap: "xy"}
参考博客:http://blog.csdn.net/qq_30100043/article/details/53422657
方法name的属性
console.log((new Function()).name) //anonymous
var doSomething = function(){ }
console.log(doSomething.bind().name); //bound
3.模块扩展
import用法,看下列代码:
import Game from "./components/Game"
import Start from "./components/Start"
import Over from "./components/Over"
import AgainFail from "./components/AgainFail"
import CommitPhone from "./components/CommitPhone"
4.HTML5之FileReader的使用
转:http://blog.csdn.net/yaoyuan_difang/article/details/38582697
5.剩余操作符(rest operator) ...
var obj1 ={
'x': 'x1',
'y': 'y1'
};
var obj2 = {
...obj1,
'z': 'z1'
};
console.log('obj1=', obj1);
console.log('obj2=', obj2);
obj1.x = 'x3';
// 深拷贝 obj2不受到obj1变化的影响。
console.log('obj1=', obj1);
console.log('obj2=', obj2);
obj2 = {
...obj1,
'y': 'y2'
};
// 重复的y成员被替换
console.log('obj1=', obj1);
console.log('obj2=', obj2);
obj2 = {
y: 'y3',
...obj1
};
//安装顺序替换
console.log('obj2=', obj2);
// obj1= { x: 'x1', y: 'y1' }
// obj2= { x: 'x1', y: 'y1', z: 'z1' }
// obj1= { x: 'x3', y: 'y1' }
// obj2= { x: 'x1', y: 'y1', z: 'z1' }
// obj1= { x: 'x3', y: 'y1' }
// obj2= { x: 'x3', y: 'y2' }
// obj2= { y: 'y1', x: 'x3' }
6.import和require的区别
1.require可以在代码的任何地方,而import只能用到文件的头部
2.require赋值功能,import是编译功能,所有import的性能要优于require。
3.当import遇到了default,于require是是完全不同的两种概念。
4.建议require使路径用绝对路径,import使用相对路径。