一、类编程
引入类的概念,让其具备面向对象的开发数组
class Person {
constructor(name,age) {
this.name = name;
this.age = age;
}
}
复制代码
二、模块化bash
模块之间的相互调用关系是经过export来规定模块对外暴露的接口,经过import来引用其它模块提供的接口并发
export var name = 'Rainbow'; //导出变量
export const sqrt = Math.sqrt; //导出常量
var name = 'Rainbow'; //导出多个变量
var age = '24';
export {name, age};
export function myModule(someArg) { //导出函数
return someArg;
}
export default class MyComponent extends Componet{ //导出组件
render(){
<Text>自定义组件</Text>
}
}
复制代码
定义好模块的输出之后就能够在另一个模块经过import引用异步
import {myModule} from 'myModule'; //导入函数
import {name,age} from 'test'; //导入变量
import MyComponent from 'MyComponent' //导入组件
复制代码
三、箭头函数async
箭头函数与包围它的代码共享同一个this,能帮你很好的解决this的指向问题模块化
()=>{
alert("foo");
}
复制代码
错误示范异步编程
class PauseMenu extends React.Component{
componentWillMount(){
AppStateIOS.addEventListener('change', this.onAppPaused.bind(this));
}
componentWillUnmount(){
AppStateIOS.removeEventListener('change', this.onAppPaused.bind(this));
}
onAppPaused(event){
}
}
复制代码
正确示范函数
class PauseMenu extends React.Component{
componentWillMount(){
AppStateIOS.addEventListener('change', this.onAppPaused);
}
componentWillUnmount(){
AppStateIOS.removeEventListener('change', this.onAppPaused);
}
onAppPaused = (event) => {
}
}
复制代码
四、函数参数默认值ui
function foo(height = 50, color = 'red'){}
复制代码
五、模板字符串
var name = `Your name is ${firstname} ${lastname}`
复制代码
六、解构赋值
从数组中获取值
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7
复制代码
交换两个变量的值
var a = 1;
var b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1
复制代码
获取对象中的值
const student = {
name:'Ming',
age:'18',
city:'Shanghai'
};
const {name,age,city} = student;
console.log(name); // "Ming"
console.log(age); // "18"
console.log(city); // "Shanghai"
复制代码
七、延展操做符
延展操做符...,能够对数组、对象、string进行展开操做
myFunction(...iterableObj); //对函数展开
[...iterableObj, '4', ...'hello', 6]; //对数组展开
let objClone = { ...obj }; //对对象展开
复制代码
对数组展开
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
var arr3 = [...arr1, ...arr2]; // 将 arr2 中全部元素附加到 arr1 后面并返回
复制代码
对对象展开
var params = {
name: '123',
title: '456',
type: 'aaa'
}
var { type, ...other } = params;
<CustomComponent type='normal' number={2} {...other} />
//等同于
<CustomComponent type='normal' number={2} name='123' title='456' />
复制代码
八、对象属性简写
简写前
const name='Ming',age='18',city='Shanghai';
const student = {
name:name,
age:age,
city:city
};
console.log(student);
复制代码
简写后
const name='Ming',age='18',city='Shanghai';
const student = {
name,
age,
city
};
console.log(student);
复制代码
九、Promise
Promise是异步编程的一种解决方案,在不使用Promise的时候须要嵌套多层代码
setTimeout(function()
{
console.log('Hello'); // 1秒后输出"Hello"
setTimeout(function()
{
console.log('Hi'); // 2秒后输出"Hi"
}, 1000);
}, 1000);
复制代码
使用Promise后,只须要经过then操做符进行操做
var waitSecond = new Promise(function(resolve, reject)
{
setTimeout(resolve, 1000);
});
waitSecond
.then(function()
{
console.log("Hello"); // 1秒后输出"Hello"
return waitSecond;
})
.then(function()
{
console.log("Hi"); // 2秒后输出"Hi"
});
复制代码
十、Let与Const
const与let都是块级做用域
{
var a = 10; // 全局做用域
}
console.log(a); // 输出10
{
let a = 10; // const或let,块级做用域
}
console.log(a); //-1 or Error“ReferenceError: a is not defined”
复制代码
一、includes()
includes()函数用来判断一个数组是否包含一个指定的值
arr.includes(x)
//等同于
arr.indexOf(x) >= 0
复制代码
二、指数操做符
引入了指数运算符**,** 具备与**Math.pow(..)**等效的计算结果
console.log(Math.pow(2, 10)); // 输出1024
console.log(2**10); // 输出1024
复制代码
一、async/await
async/await是异步函数,结合Promise,在使用上使整个代码看起来很简洁
//登录用户
login(userName) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('1001');
}, 600);
});
}
//获取用户数据
getData(userId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (userId === '1001') {
resolve('Success');
} else {
reject('Fail');
}
}, 600);
});
}
// 不使用async/await
doLogin(userName) {
this.login(userName)
.then(this.getData)
.then(result => {
console.log(result)
})
}
// 使用async/await
async doLogin(userName) {
const userId=await this.login(userName);
const result=await this.getData(userId);
}
doLogin('Hensen').then(console.log); //经过then获取异步函数的返回值
复制代码
async/await支持并发操做,咱们经过Promise.all来实现await的并发调用
async function charCountAdd(data1, data2) {
const [d1,d2]=await Promise.all([charCount(data1),charCount(data2)]);
return d1+d2;
}
charCountAdd('Hello','Hi')
.then(console.log)
.catch(console.log); //捕捉整个async/await函数的错误
function charCount(data) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(data.length);
}, 1000);
});
}
复制代码
二、Object.values()
遍历对象的全部值
const obj = {a: 1, b: 2, c: 3};
const vals=Object.keys(obj).map(key=>obj[key]);
console.log(vals);//[1, 2, 3]
const values=Object.values(obj1);
console.log(values);//[1, 2, 3]
复制代码
三、Object.entries()
遍历对象的全部key和value
Object.keys(obj).forEach(key=>{
console.log('key:'+key+' value:'+obj[key]);
})
for(let [key,value] of Object.entries(obj1)){
console.log(`key: ${key} value:${value}`)
}
复制代码
四、String padding
PadStart和PadEnd函数可向左、右填充字符串(如空格),若是目标长度过短则不填充,若是目标长度有多余的空位,则填补参数padString的值
// String.prototype.padStart(targetLength [, padString])
'hello'.padStart(10); // ' hello'
'hello'.padStart(10, '0'); // '00000hello'
'hello'.padStart(); // 'hello'
'hello'.padStart(6, '123'); // '1hello'
'hello'.padStart(3); // 'hello'
'hello'.padStart(3, '123'); // 'hello';
// String.prototype.padEnd(targetLength [, padString])
'hello'.padEnd(10); // 'hello '
'hello'.padEnd(10, '0'); // 'hello00000'
'hello'.padEnd(); // 'hello'
'hello'.padEnd(6, '123'); // 'hello1'
'hello'.padEnd(3); // 'hello'
'hello'.padEnd(3, '123'); // 'hello';
复制代码
五、函数参数列表结尾容许逗号
var f = function(a,b,c,d,) {}
复制代码
六、Object.getOwnPropertyDescriptors()
Object.getOwnPropertyDescriptors()获取一个对象的全部自身属性的描述符
const obj2 = {
name: 'Jine',
get age() { return '18' }
};
Object.getOwnPropertyDescriptors(obj2)
复制代码