A. Es6 build environment to build
1. Create a package.json initialization file npm
npm init
2. Install webpack
cnpm install webpack -D
3. Install babel related packages to compile grammar es6
cnpm install babel-loader babel-core babel-preset-es2015 -D
Second, write webpack.config.js configuration files, configuration compiler es6
1. loader configuration
module.exports = {
entry:'./entry.js',
output:{
filename:'./bundle.js'
},
module:{
loaders:[
{
test:/\.js$/,
loader:'babel-loader',
exclude:/node_modules/
}
]
}
}
2. Create a profile .babelrc
{
"presets": ["es2015"]
}
Li three times, es6 and alternative anguments ...
1 over Li target and substitute anguments
{
function test3(...arg){
for(let v of arg){
console.log("rest",v);
}
}
test3(1,2,3,4,5,"a");
}
2. es6 of objects over Li, Object.entries
{
let test ={x:1,y:456};
for(let [key,value] of Object.entries(test)){
console.log([key,value]);
}
}
Fourth, the inherited class class.
Added: common method is to instantiate an object out of the static methods belong to the class, also it can be inherited.
1. The basic definition of the class to generate an instance
{
class Person{
//构造函数。
constructor(name = "laozhou"){ //默认值:laozhou
this.name = name;
}
}
let p1 = new Person("小王"); //new的时候自动执行构造函数。
console.log("构造函数和实例",p1);
}
2. Inheritance
extends 继承
super 上一级,可以调用父类的构造函数。
{
class Father {
constructor(name="侯瑞强",sex="男") {
this.name = name;
this.sex = sex;
}
}
class Child extends Father {
constructor(name="child",sex) { //把父类的本事拿了过来。
super(name,sex); //调用父类的构造函数。super必须在第一行,否则报错。
this.age = 10;
}
}
console.log("子类覆盖父类属性的实例",new Child());
}
3. static properties
{
class Person {
constructor(name="默认") {
this.name = name;
}
}
//静态属性的定义,是直接给类下的属性赋值,该属性就是静态属性,类名点什么直接定义
Person.type = "text"; //type就是静态属性。
console.log(Person.type);
}
Fifth, modular
1. Export exprot, introduced import
Export
export default{
a:1,
b:2,
say(){
console.log("i can say");
}
}
Importing
import Model2 from "./module2.js";
console.log(Model2);
Sixth, tail call
一个函数执行,执行到最后的时候调用了另一个函数。
function go(callback){
console.log(1231313);
console.log("vvv");
callback && callback();
}
function tail(){
console.log(123131313);
}
go(tail);