ES6中for迴圈 函式擴充套件 字串擴充套件 建立類

2021-09-29 04:26:05 字數 2568 閱讀 7356

1.for:

var arr =

['a'

,'b'

,'c'];

console.

log(

"for:");

for(

var i =

0; i < arr.length; i++

);

2.foreach:
var arr =

['a'

,'b'

,'c'];

console.

log(

"***************");

console.

log(

"foreach:");

//遍歷:陣列 引數一:當前元素 引數二:當前元素的索引值

arr.

foreach

(function

(term, index)

);

3.for in:
var arr =

['a'

,'b'

,'c'];

console.

log(

"***************");

console.

log(

"for in:");

for(

var index in arr)

4.for of:
console.

log(

"***************");

console.

log(

"for of:");

//遍歷:陣列,字串,對映,集合等資料結構 只取出值,

varset

=new

set([1

,2,3

,4])

;//for of獲取值

for(

var val of

set)

var a =

['g'

,'d'

,'j'];

//for of獲取陣列索引

//keys():從陣列中建立迭代物件,該物件包含了陣列的鍵;返回的是乙個迭代物件

for(

var index of arr.

keys()

)

1.es6以後函式的引數可以寫預設值
function

f(name =

"jhh"

, age =20)

f();

2.箭頭函式:
/*

箭頭函式:

當函式只有乙個引數時,小括號可以不寫;當函式體只有一句**的時候大括號可以不寫

*///1.沒有引數

varfn=(

)=> console.

log(

"jhhhhhhhhh");

fn();

//2.有乙個引數

varfn2

= a => console.

log(a)

;fn2(1

);//3.有多個引數 函式體有多行**

varfn3

=(a,b)

=>

;fn3(1

,2);

4.rest和arguments:
/*

arguments:獲取所有實參;

rest:獲取函式多餘的引數;

es6語法以後rest替換了arguments;

rest相較於arguments更靈活

*/function

fn4(

) console.

log(arguments);}

fn4(1,

2,3,

5,4)

;//第乙個引數賦值給a,剩下的引數全部賦值給vals

function

fn5(a,

...vals)

fn5(1,

2,3,

5,4)

;

1.repeat函式:指定平鋪的次數
var str =

"chun"

;console.

log(str.

repeat(3

));

2.模板字串:傳統js中通過 「+」 拼接html,現在使用反引號;需要新增變數直接${}
var a =

"hh"

;var html =

` `;

console.

log(html)

;

//建立乙個類

class

person}}

var person =

newperson

("jhh",20

);console.

log(person)

;console.

log(person.

sayname()

);

ES6 函式擴充套件

函式在js裡是相當重要的一部分了,es6裡也新增了一些函式方法,來看一下 test hello hello world test hello kill hello kill es6增加了函式引數預設值,可以直接在宣告引數的同時賦預設值,但是也可以後面重新賦值 test2 kill 括號內有引數x時,...

ES6函式擴充套件

函式引數的預設值 在es5中,我們想給函式乙個預設值,需要這樣寫 function add x,y 在es6中 可以這樣寫 function add x,y ss add dd ddss add ss dd ssdd add dd dd我們只需要在引數上直接寫上我們想要的預設值就好了。當我們給函式乙...

ES6函式擴充套件

function fun a,b world fun hello 輸出helloworld let a aa function fun a,b a fun bb function fun arg fun 1,2,3,4,1 語法 param param 對應函式 function 沒有引數 乙個引數...