{
"name": "webpack-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-latest": "^6.24.1",
"webpack": "^3.11.0"
}
}
开启一个本地服务
npm install http-server -g
http-server -p 8881
Promise语法
function loadImg(src){
const promise = new Promise(function(resolve,reject){
var img = document.createElement('img');
img.onload = function(){
resolve(img);
}
img.onerror = function(){
reject()
}
img.src = src
})
return promise;
}
var src = '#';
result.then(function (img) {
console.log(img.width);
}, function () {
console.log('failed');
})
result.then(function (img) {
console.log(img.height);
})
new Promise 实例,而且要return
new Promise时传入函数,函数resolve reject 两个参数
成功时执行resolve()失败执行reject()
then监听结果
// let / const
// JS
var i = 10;
i = 100;
var j = 20;
j = 200;
// ES6
let i = 10;
i = 100; //正确
const j = 20;
j = 200; //报错
// 多行字符串和模板解析
// js
var name = 'zhansan',
age = 20,
html = '';
html += '<div>';
html += ' <p>' + name + '</p>';
html += ' <p>' + age + '</p>';
html += '</div>';
// es6
const name = 'zhansan',age =20;
const html = `
<div>
<p>${name}</p>
<p>${age}</p>
<div>
`;
// 解构赋值
// js
var obj = {a:100,b:200}
var a = obj.a;
var b = obj.b;
var arr = ['xxx','yyy','zzz'];
var x = arr[0];
var z = arr[0];
// es6
const obj = {a:100,b:200};
const {a,b} = obj;
const arr = ['xxx','yyy','zzz'];
const [x,y,z] = arr;
// 块级作用域
// js
var obj = {a:100,b:200}
for(var item in obj){
console.log(item)
}
console.log(item); 'b'
// es6
const obj = {a:100,b:200}
for(let item in obj){
console.log(item)
}
console.log(item); //undefined
// 函数默认参数
// js
function a(a,b){
if(b == null){
b=0
}
}
// es6
function a(a,b=0){
}
// 箭头函数
var arr = [1,2,3];
arr.map(function(item){
return item + 1
})
const arr = [1,2,3,4];
arr.map(item => item +1);
arr.map((item,index) => {
console.log(index);
return item + 1;
})
function fn(){
console.log('real',this); //{a:100}
var arr = [1,2,3];
// 普通 JS
arr.map(function(item){
console.log('js',this); //window
return item + 1;
})
// 箭头函数
arr.map(item =>{
console.log('es6',this);//{a:100}
return item + 1;
})
}
fn.call({a:100})
总结-es6常用功能
let/const
多行字符串/模板变量
解构赋值
块级作用域
函数默认参数
箭头函数
网友评论