ES6.x
程序员文章站
2022-05-18 20:44:04
类 模块化 箭头函数 模板字符串 let name=Tom let [foo, [[bar], baz]] = [1, [[2], 3]]; foo // 1 bar // 2 baz // 3 [x, y = 'b'] = ['a', undefined]; // x='a', y='b' for ......
类
class animal{ constructor(name){ this.name=name } spack(){ console.log(name) } } class dog extends animal{ constructor(name,age){ super(name);//子类中如果有constructor,就必须有super this.age=age } spack(){ super.spack()//如果没有,则重写spack方法,有则在spack中添加内容 console.log(age) } }
模块化
//导出 var name = 'rainbow'; var age = '24'; export {name, age}; 导入 import {name,age} from '来源';
箭头函数
()=>1 v=>v+1 (a,b)=>a+b ()=>{ alert("foo"); } e=>{ if (e == 0){ return 0; } return 1000/e; }
模板字符串
let name=tom `welcome ${name}`
解构赋值
let [foo, [[bar], baz]] = [1, [[2], 3]]; foo // 1 bar // 2 baz // 3 [x, y = 'b'] = ['a', undefined]; // x='a', y='b' for (let [key, value] of map) { console.log(key + " is " + value); }
延展操作符
function sum(x, y, z) { return x + y + z; } const numbers = [1, 2, 3]; console.log(sum(...numbers)); let [a,b,...c] = [1,2,3,4,5,6,7,] console.log(a,b,c)//1 2 [3,4,5,6,7] var arr = [1, 2, 3]; var arr2 = [...arr]; // 等同于 arr.slice()
promise
var promise = new promise(function(resolve, reject) { // ... some code if (/* 异步操作成功 */){ resolve(value); } else { reject(error); } }); promise.then(function(value) { // success }, function(error) { // failure }); promise.then((value) => { // success }, (error) => { // failure }); promise.then((value) => { // success }).catch((err) => { //failure });
generator函数
function * gen(x){ var y = *yield* x + 2; return y; } var g = gen(1); g.next() // { value: 3, done: false } g.next(2) // { value: 2, done: true }
async/await
*async* function process(array) { for *await* (let i of array) { dosomething(i); } } async function process(array) { for await (let i of array) { dosomething(i); } }
正则
/foo.bar/.test('foo\nbar')// false /foo.bar/s.test('foo\nbar') // true let str = '2019-07-31' let reg = /(\d{4})-(\d{2})-(\d{2})/ let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ console.log(str.match(reg).groups) let reg = /^(?<name>welcome)-\k<name>-\1$/ let str = welcome-welcome-welcome console.log(reg.test(str))
proxy
let json = { name, age } let newjson = new proxy(json,{ set(target,property,value){ if(property == 'age'){ if(!number.isinteger(value)){ <!-- throw new typeerror('年龄是整数') --> return false } } target[property] = value return true }, get(target,property){ if(property in target){ console.log(`你访问了${property}属性`) return target[property] }else{ console.log(`没有这个属性`) return null } }, has(target,key){ return key in target; }, deleteproperty(target,property){ return true } }) var target = function () { return 'i am the target'; }; var handler = { apply: function () { return 'i am the proxy'; } }; var p = new proxy(target, handler); p() const dom = new proxy({},{ get(target,property){ return function(attr={},...children){ console el = document.createelement(property) for(let key of object.keys(attr)){ el.setattribute(key,attr[key]) } for(let child of children){ if(typeof child == 'string'){ child = document.createtextnode(child) } el.appendchild(child) } return el } } }) let odiv = dom.div( {id:'id1',class:'cl1'},'div','1123', dom.ul({}, dom.li({},111) dom.li({},222) dom.li({},333) ) )