ES6(常用的、重點的)
數組API、正則API、字符串API都不講
模板字符串
var s1 = `abc`
var obj= { name: "" , age: 5 } ;
var s1 = "我叫:" + obj. name+ ",今年:" + obj. age+ "歲。"
解構賦值
var obj= { name: "張三" , age: 18 }
var { name, age} = obj;
var { name: title} = obj;
function f1 ( obj) {
console. log ( obj. age) ;
console. log ( obj. height)
}
function f1 ( { age, height } ) {
console. log ( age) ;
console. log ( height)
}
f1 ( { age: 5 , height: 180 } )
var a = 3 ;
var c = 10 ;
var b = { a, c } ;
console. log ( b)
函數的擴展
rest參數
使用背景:es6的
優點:arguments是僞數組,而rest參數是真數組
function fn ( ... args) {
console. log ( args) ;
}
fn ( 1 , 2 , 3 , 4 , 5 )
箭頭函數
div. onclick = function ( ) {
console. log ( "你好" )
}
div. onclick = ( ) => {
console. log ( "你好" )
}
var fn = ( a) => {
console. log ( "abc" ) ;
}
var fn = a=> {
console. log ( "abc" ) ;
}
var f = ( a, b, c) => {
console. log ( "abc" )
}
箭頭函數和普通匿名函數有哪些不同?
函數體內的this對象,就是定義時所在的對象,而不是使用時所在的對象。
不可以當作構造函數,也就是說,不可以使用new命令,否則會拋出一個錯誤。
不可以使用arguments對象,該對象在函數體內不存在。如果要用,可以用 rest 參數代替。
(不常用)不可以使用yield命令,因此箭頭函數不能用作 Generator 函數。
對象的擴展
Object.assign:實現拷貝繼承
對象擴展運算符
var obj1= { age: 5 , gender: "男" }
var obj2= { ... obj1 }
var obj3= { ... obj1 , age: 10 }
Promise
爲什麼要有promise:解決回調地獄的問題
回調地獄:
$. get ( "/getUser" , function ( res) {
$. get ( "/getUserDetail" , function ( ) {
$. get ( "/getCart" , function ( ) {
} )
} )
} )
Promise函數基本用法
var promise= new Promise ( ( resolve, reject) => {
$. get ( "/getUser" , res=> {
resolve ( res)
} )
} )
promise. then ( res=> {
console. log ( res) ;
} )
Promise函數實現多層回調
new Promise ( ( resolve, reject) => {
$. get ( "/getUser" , res=> {
resolve ( res)
} )
} ) . then ( res=> {
return new Promise ( resolve=> {
$. get ( "/getUserDetail" , res=> {
resolve ( res)
} )
} )
} ) . then ( res=> {
return new Promise ( resolve=> {
$. get ( "/getCart" , res=> {
resolve ( res)
} )
} )
} ) . then ( res=> {
} )
Promise函數錯誤處理
new Promise ( ( resolve, reject) => {
$. ajax ( {
url: "/getUser" ,
type: "GET" ,
success: res=> {
resolve ( res) ;
} ,
error: res=> {
reject ( res)
}
} )
} ) . then ( resSuccess=> {
} , resError=> {
} )
new Promise ( ( resolve, reject) => {
$. ajax ( {
url: "/getUser" ,
type: "GET" ,
success: res=> {
resolve ( res) ;
} ,
error: res=> {
reject ( res)
}
} )
} ) . then ( resSuccess=> {
} ) . catch ( resError=> {
} )
async
async function get ( ) {
console. log ( '開始執行' ) ;
var res = await timer ( )
console. log ( '執行結束:' , res) ;
}
function timer ( ) {
return new Promise ( ( resolve, reject) => {
setTimeout ( ( ) => {
resolve ( "你好" ) ;
} , 1000 )
} )
}
get ( ) ;
class
定義一個類
class Person {
constructor ( name) {
this . name= name;
}
}
function Person ( name) {
this . name= name;
}
添加實例方法
class Person {
constructor ( name, age) {
this . name= name;
this . age= age;
}
say ( ) {
console. log ( "大家好,我叫:" + this . name+ ",今年:" + this . age+ "歲" ) ;
}
travel ( ) {
console. log ( "坐着飛機去巴厘島" ) ;
}
}
添加靜態方法
class Animal {
constructor ( ) {
}
static born ( ) {
console. log ( "小呆萌出生了" )
}
}
Animal. born ( ) ;
類的繼承
class Person {
constructor ( name) {
this . name= name;
}
}
class Student extends Person {
constructor ( name, grade) {
super ( name) ;
}
}
module
基本用法
export default { name: "abc" }
import common from "common.js"
console. log ( common. name )
模塊有多個導出
export const jim = { country : "France" }
export const tony = { color: "gray" }
export default { name: "abc" }
import person , { jim , tony } from "person.js"
模塊導入導出取別名
export const tony = { color: "gray" }
export { tony as Tony }
import { Tony } from "person.js"
import { Tony as man} from "person.js"
console. log ( man)