let obj = {a: 1, b: 2, c: 3}; for (let v in obj) { console.log(v); }
The for...in loop should NOT be used to iterate over arrays because, depending on the JavaScript engine, it could iterate in an arbitrary order. Also, the iterating variable is a string, not a number, so if you try to do any math with the variable, you'll be performing string concatenation instead of addition.
ES6 introduces the new for...of loop, which creates a loop iterating over iterable objects. For example:
ES6引入新的 for...of迴圈,新增可迭代物件的迴圈迭代,例如:
let list = ["x", "y", "z"]; for (let val of list) { console.log(val); }
During each iteration the val variable is assigned the corresponding element in the list.
在每次的迭代當中,val變數被指派為list陣列中相應的元素。
The for...of loop works for other iterable objects as well, including strings.
for...of迴圈也可以用於迭代物件,包含字串。
for (let ch of "Hello") { console.log(ch); }
TIP:The for...of loop also works on the newly introduced collections (Map, Set, WeakMap, and WeakSet). We will learn about them in the upcoming lessons. Note that ES6 code will run only in browsers that support it. Older devices and browsers that do not support ES6 will return a syntax error.
Prior to ES6, a JavaScript function was defined like this:
在ES6之前,JavaScript的函式是像這樣被定義的:
function add(x, y) { var sum = x+y; console.log(sum); }
ES6 introduces a new syntax for writing functions. The same function from above can be written as:
ES6引入一個寫函式的新語法,跟上方一樣的函式可以被寫成:
const add = (x, y) => { let sum = x + y; console.log(sum); }
This new syntax is quite handy when you just need a simple function with one argument. You can skip typing function and return, as well as some parentheses and braces. For example:
The code above defines a function named greet that has one argument and returns a message.
上面的程式碼定義了一個稱為greet的函式,它有一個引數並回傳一則訊息。
If there are no parameters, an empty pair of parentheses should be used, as in
如果沒有參數(parameters),應該要使用一對空的小括弧,像這樣:
const x = () => alert("Hi");
The syntax is very useful for inline functions. For example, let's say we have an array, and for each element of the array we need to execute a function. We use the forEach method of the array to call a function for each element:
TIP:The code is shorter and looks pretty nice, doesn't it?
提示:程式碼更短而且看起來非常棒,不是嗎?
-----
Default Parameters in ES6
ES6中的預設參數
In ES6, we can put the default values right in the signature of the functions. For example:
在ES6當中,我們可以直接將預設值放在函式的簽名中,例如:
function test(a, b = 3, c = 42) { return a + b + c; } console.log(test(5)); //50
And here's an example of an arrow function with default parameters:
這是箭頭函式使用預設參數的範例:
const test = (a, b = 3, c = 42) => { return a + b + c; } console.log(test(5)); //50
TIP:Default value expressions are evaluated at function call time from left to right. This also means that default expressions can use the values of previously-filled parameters.
提示:在函式呼叫時,預設值表達式從左到右被評估,這也表示預設的表達式可以使用預先填入的參數值。
-----
翻譯小心得| 這也是深入淺出的一篇,把ES6新的方法及語法用淺顯的說明和範例來解釋,非常實用。 Finally, the article above is from SOLOLEARN . I translated it just for practice, personal use only. If that is inappropriate, please contact me.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.