建设一个小网站赚钱吗,高端网站设计工作室,小程序开发文档微信小程序,石家庄 外贸网站建设ES6新语法 1 let定义变量2 const定义常量3 模板字符串4 方法默认值5 箭头函数6 解构6.1 对象解构6.2 数组解构6.2 使用解构实现变量交换 7 Spread Operator8 模块化编程 1 let定义变量 使用let定义变量能更加精准的确定变量的作用域 //for(var i 0 ; i 10 ; i){}
for(let… ES6新语法 1 let定义变量2 const定义常量3 模板字符串4 方法默认值5 箭头函数6 解构6.1 对象解构6.2 数组解构6.2 使用解构实现变量交换 7 Spread Operator8 模块化编程 1 let定义变量 使用let定义变量能更加精准的确定变量的作用域 //for(var i 0 ; i 10 ; i){}
for(let i 0 ; i 10 ; i){}
console.log(i); 2 const定义常量 使用const定义常量常量一旦定义不可以改变 const a 10;
a 20;//Assignment to constant variable.3 模板字符串 使用模板字符串可以避免大量的字符串拼接 var name cxk;
console.log(欢迎name登录中...);//字符串拼接
console.log(欢迎${name}登录中...);//模板字符串4 方法默认值 类似于springmvc中接受参数拥有默认值 function f1(a10 , b{name:haha}){console.log(a);console.log(b);
}
f1(); //没有传递参数则使用默认值
f1(a,b); //传递参数则使用传递的参数5 箭头函数 使用箭头函数可以简化复杂的代码结构(类似于java中lambda表达式) //使用ES5的语法定义函数
//var f1 function (a,b){
// return ab;
// }
//使用ES6的语法定义函数
var f2 (a,b) ab
console.log(f2(10,20));箭头函数应用 //1、定义数组获取数组中所有的偶数
let arr [1,2,3,4,5,6,7];
//使用传统方式
// arr arr.filter(function(num){
// if(num % 2 0){
// return num;
// }
// })
//使用箭头函数
arr arr.filter(num num % 2 0)
console.log(arr);//2、定义数组获取名称包含‘a’字符且长度大于6的元素
let arr [zhangsan,lisi,wangwu,zhaoliu];
//使用传统方式
// arr arr.filter(function(name){
// if(name.indexOf(a) 0 name.length 6){
// return name;
// }
// })
//使用箭头函数
arr arr.filter(name name.indexOf(a)-1 name.length 6 );
console.log(arr);6 解构 定义从一个大的数组或对象中提取个别值使用 6.1 对象解构
//对象解构
//获取user对象中的name、age属性let user {name:zs,age:20,sex:男};
// let name user.name;
// let age user.age;
// console.log(name,age);//使用解构
// let {name,age} user;
// console.log(name,age)//如果新对象的属性名不一致的时候需要指定名称
let {name:name1,age:age1} user;
console.log(name1,age1)
6.2 数组解构
//数组解构
let produts [{name:小米,price:3999},{name:华为,price:4999},{name:苹果,price:6999},{name:三星,price:5999},]let p1,p2;
[p1,p2] produts;
console.log(p1);
console.log(p2);
//取出第三第四个对象
[,,p1,p2] produts;
console.log(p1,p2);6.2 使用解构实现变量交换
let a 10;
let b 20;
[a,b] [b,a];
console.log(a,b);7 Spread Operator 通过不定参数实现解构 常用于JSON对象 //数组拼接
let arr1 [1,3,4,5];
let arr2 [100,200];
let arr3 [...arr1,...arr2];
console.log(arr3);//对象
let user {name:jack,gender:男};
let userInfo {...user,age:30};
console.log(userInfo);8 模块化编程 使用模块化编程可以减少大量的js库的引入拆分的功能相互独立可以单独测试(java中的解耦) 注意的点: 使用export default 向外暴露的成员可以使用任意的变量来接收在一个模块中export default 只允许向外暴露1次在一个模块中可以同时使用 export default 和 export 向外暴露成员(方法、变量、对象)目前浏览器上还不支持ES6的导入导出语法。需要在设置js的类型为module script typemodule 导出模块 //导出add方法default表示默认方法(有且仅有一个default导出)
export default function add(a,b){return ab;
}
//导出其他方法
export function f1(){console.log(导出f1函数);
}//导出变量
export let username admin;//导出对象
export let user {name:zs,age:30
}导入模块 没有使用default修饰的内容必须写在{}中且名称保持一致导入default模块可以自己制定名字 script typemodule//导入模块 导入default模块可以自己制定名字。其他的导入需要方法{}中且名称保持一致import add2,{f1,username,user} from ./js/test.js;console.log(add2(10,20));f1();console.log(username);console.log(user.name,user.age);
/script
文章转载自: http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.xfxnq.cn.gov.cn.xfxnq.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.njqpg.cn.gov.cn.njqpg.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.psqs.cn.gov.cn.psqs.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.zjrnq.cn.gov.cn.zjrnq.cn http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn