【ES5 ES6】使用学习
转载:
=============================================================
1、Promise
2、下划线转驼峰,驼峰转下划线
=============================================================
1、Promise
基本使用
const promise = new Promise(function (resolve, reject) { console.log('pending ...'); let random = parseInt(Math.random() * 10); let response = random % 2 == 0 ? true : false; if (response) { resolve(response); } else { reject(response); } }); promise.then(response => { console.log('success: ' + response); }, error => { console.log('failure: ' + error); });
2、下划线转驼峰,驼峰转下划线
// 下划线转驼峰
"name_face".replace(/_(w)/g,function ($0,$1){return $1.toUpperCase();});
// 驼峰转下划线
"nameFace".replace(/([A-Z])/g,"_$1").toLowerCase();