es6常用方法

2021-07-29 17:17:45 字數 2894 閱讀 5683

1.let , const

const

es6中引入了塊級作用域

2.解構賦值

// var a = 1;

// var b = 2;

// var c = 3;

// var a = 4,b = 5,c = 6;

// console.log(a,b,c);

// let [a,b,c] = [123,456,789];

// let [a,,c] = [123,789];

// let [a,[num,abc],c] = [123,[111,123],789];

// let [a=1,b,c] = [,123,];

// console.log(a,b,c);

// 交換x和y的值

// let [x,y] = [1,2];

// [x,y] = [y,x];

// console.log(x,y);

// let  = ;

// let = ;

// console.log(username,age);

// var obj =

// };

// let = obj;

// console.log(name,age,friend.fname,friend.***);

// let [a,b,c,d,e,f,length] = 'hello';

// console.log(a,b,c,d,e,f,length);

// console.log('hello'.length);

3.includes(),startswith(),endswith()

//

let str = 'hello world';

'world',7));

//

let url = 'admin/index.php';

'index',7));

'.html'));

4.模板字串

var tag = `

$span>

$span>

$span>

$span>

div>

`;console.log(tag);

5.引數

函式引數的預設值

// function

foo(abc = 'hi')

// foo('hello');

引數支援變數的解構賦值

// function

foo( = {})

// foo();

// foo();

rest 引數

// arguments

// function foo(a,...data)

// function foo(...data)

// foo(1,2,3,4,5,6);

擴充套件運算子 …

// let arr = [1,2,3,4,5,6];

// function foo(a,b,c,d,e)

// // foo(...arr);

陣列合併

//

let arr1 = ['red','green'];

//let arr2 = ['blue','orange','yellow'];

//let arr = [...arr1,...arr2];

6.箭頭函式

//

letfoo = () =>

console.log('hello world');

箭頭函式中注意事項:

7.類和繼承(建構函式)

// class person

// showweight()

// show***()

// }

// let p = new person('female','75kg');

// p.showweight();

// p.show***();

// class person

// constructor(***,weight)

// showweight()

// show***()

// }

// let p = new person('female','75kg');

// p.showweight();

// p.show***();

// // p.showinfo();

// person.showinfo();

// class student extends person

// showscore()

// }

// let stu = new student('male','70kg','100');

// stu.showscore();

// stu.show***();

// stu.showweight();

// student.showinfo();

ES6新增常用方法

let 1.在塊級作用域內部有效 迴圈中迴圈變數部分和迴圈體內部是不同作用域 不存在變數提公升,先宣告後使用,否則報錯 4.塊級作用域內只要存在let,所生命的變數就 繫結 這個區域,不受外界影響 var a for let i 1 i 10 i a 6 6塊級作用域與函式宣告 允許在塊級作用域內宣...

ES6新增常用方法

let區別1 let定義的變數只能在 塊 裡面使用,形成乙個 塊級作用域 var作用於函式作用域 區別2 let不允許重複宣告,它是乙個封閉空間,解決了i問題 var a 5 var a 12 a 5 let a 5 let a 12 error 區別3 const const用來定義常量,一旦定義...

ES6新增,常用的方法

新增了canvas 繪畫檢視 新增了定義class的語法糖 函式的新增了箭頭函式 函式引數的預設值 陣列例項新增了 keys values entries 新增了基本資料型別 symbol 變數的解構賦值 新增了map資料結構 和 set資料結構 新增了模組化 import export 陣列和物件...