From 6ca84f1794af830ada9736d7289ce29aabb04ca3 Mon Sep 17 00:00:00 2001 From: t7yang Date: Mon, 10 Jan 2022 08:38:05 +0800 Subject: remove `notranslate` class in zh-TW --- files/zh-tw/web/javascript/closures/index.html | 36 ++++----- .../web/javascript/data_structures/index.html | 4 +- .../guide/expressions_and_operators/index.html | 86 +++++++++++----------- .../javascript/guide/numbers_and_dates/index.html | 28 +++---- .../inheritance_and_the_prototype_chain/index.html | 22 +++--- .../web/javascript/memory_management/index.html | 14 ++-- .../reference/classes/constructor/index.html | 8 +- .../reference/functions/arguments/index.html | 36 ++++----- .../reference/functions/arrow_functions/index.html | 40 +++++----- .../functions/default_parameters/index.html | 22 +++--- .../global_objects/array/length/index.html | 10 +-- .../global_objects/array/slice/index.html | 14 ++-- .../global_objects/date/getday/index.html | 6 +- .../reference/global_objects/math/pow/index.html | 4 +- .../global_objects/math/random/index.html | 10 +-- .../global_objects/number/tofixed/index.html | 4 +- .../global_objects/object/assign/index.html | 22 +++--- .../reference/global_objects/object/index.html | 12 +-- .../reference/global_objects/proxy/index.html | 18 ++--- .../reference/global_objects/set/index.html | 12 +-- .../operators/conditional_operator/index.html | 8 +- .../reference/operators/spread_syntax/index.html | 36 ++++----- .../reference/statements/if...else/index.html | 18 ++--- .../javascript/reference/statements/let/index.html | 26 +++---- 24 files changed, 248 insertions(+), 248 deletions(-) (limited to 'files') diff --git a/files/zh-tw/web/javascript/closures/index.html b/files/zh-tw/web/javascript/closures/index.html index 5dbd00dbff..f9ed8c84f7 100644 --- a/files/zh-tw/web/javascript/closures/index.html +++ b/files/zh-tw/web/javascript/closures/index.html @@ -12,7 +12,7 @@ translation_of: Web/JavaScript/Closures

思考這個例子:

-
function init() {
+
function init() {
   var name = "Mozilla"; // name 是個由 init 建立的局部變數
   function displayName() { // displayName() 是內部函式,一個閉包
     alert(name); // 使用了父函式宣告的變數
@@ -32,7 +32,7 @@ init();

再思考這個例子:

-
function makeFunc() {
+
function makeFunc() {
   var name = "Mozilla";
   function displayName() {
     alert(name);
@@ -52,7 +52,7 @@ myFunc();
 
 

這裡有個更有趣的例子:makeAdder 函式:

-
function makeAdder(x) {
+
function makeAdder(x) {
   return function(y) {
     return x + y;
   };
@@ -81,7 +81,7 @@ console.log(add10(2)); // 12
 
 

例如,假設我們想在網頁上,加個能調整文字大小的按鈕。其中一個方法是用像素指定 body 元素的 font-size,接著透過相對的 em 單位,設置其他頁面的其他元素(如 headers)個大小:

-
body {
+
body {
   font-family: Helvetica, Arial, sans-serif;
   font-size: 12px;
 }
@@ -99,7 +99,7 @@ h2 {
 
 

以下是 JavaScript:

-
function makeSizer(size) {
+
function makeSizer(size) {
   return function() {
     document.body.style.fontSize = size + 'px';
   };
@@ -112,12 +112,12 @@ var size16 = makeSizer(16);
 
 

size12size14size16 現在變成能調整字體大小到 12、14、與 16 像素的函式。而我們能如下例一般,把他們附加到按鈕上(本例為連結):

-
document.getElementById('size-12').onclick = size12;
+
document.getElementById('size-12').onclick = size12;
 document.getElementById('size-14').onclick = size14;
 document.getElementById('size-16').onclick = size16;
 
-
<a href="#" id="size-12">12</a>
+
<a href="#" id="size-12">12</a>
 <a href="#" id="size-14">14</a>
 <a href="#" id="size-16">16</a>
 
@@ -132,7 +132,7 @@ document.getElementById('size-16').onclick = size16;

以下展示如何使用閉包來定義一個能夠訪問私有函式與變數的公開函式。這種閉包的用法稱為模組設計模式(module pattern)。

-
var counter = (function() {
+
var counter = (function() {
   var privateCounter = 0;
   function changeBy(val) {
     privateCounter += val;
@@ -168,7 +168,7 @@ console.log(counter.value()); // logs 1
 

你應該也發現到我們定義了建立 counter 的匿名函式、而我們接著呼叫它,並給counter 變數指派了回傳值。我們也能在分離的變數 makeCounter 儲存函式並用其建立數個 counter。

-
var makeCounter = function() {
+
var makeCounter = function() {
   var privateCounter = 0;
   function changeBy(val) {
     privateCounter += val;
@@ -207,13 +207,13 @@ alert(counter2.value()); /* Alerts 0 */
 
 

在 ECMAScript 2015 導入 let 前,迴圈內建立的閉包,常會發生問題。請思考以下的範例:

-
<p id="help">Helpful notes will appear here</p>
+
<p id="help">Helpful notes will appear here</p>
 <p>E-mail: <input type="text" id="email" name="email"></p>
 <p>Name: <input type="text" id="name" name="name"></p>
 <p>Age: <input type="text" id="age" name="age"></p>
 
-
function showHelp(help) {
+
function showHelp(help) {
   document.getElementById('help').innerHTML = help;
 }
 
@@ -245,7 +245,7 @@ setupHelp();
 
 

其中一個解法是使用更多閉包,尤其要使用前述的函式工廠:

-
function showHelp(help) {
+
function showHelp(help) {
   document.getElementById('help').innerHTML = help;
 }
 
@@ -277,7 +277,7 @@ setupHelp();
 
 

用匿名閉包來實做的另一種方法是:

-
function showHelp(help) {
+
function showHelp(help) {
   document.getElementById('help').innerHTML = help;
 }
 
@@ -302,7 +302,7 @@ setupHelp();

如果你不想用更多閉包的話,你可以使用 ES2015 的 let 關鍵字:

-
function showHelp(help) {
+
function showHelp(help) {
   document.getElementById('help').innerHTML = help;
 }
 
@@ -334,7 +334,7 @@ setupHelp();
 
 

思考一下這個例子:

-
function MyObject(name, message) {
+
function MyObject(name, message) {
   this.name = name.toString();
   this.message = message.toString();
   this.getName = function() {
@@ -349,7 +349,7 @@ setupHelp();
 
 

上面的程式碼並未利用閉包的益處,所以,可以改成這個樣子:

-
function MyObject(name, message) {
+
function MyObject(name, message) {
   this.name = name.toString();
   this.message = message.toString();
 }
@@ -365,7 +365,7 @@ MyObject.prototype = {
 
 

但我們不建議重新定義原型,因此這個附加到現有原型的例子更佳:

-
function MyObject(name, message) {
+
function MyObject(name, message) {
   this.name = name.toString();
   this.message = message.toString();
 }
@@ -379,7 +379,7 @@ MyObject.prototype.getMessage = function() {
 
 

以上的程式碼,可以寫得如同下例般簡潔:

-
function MyObject(name, message) {
+
function MyObject(name, message) {
     this.name = name.toString();
     this.message = message.toString();
 }
diff --git a/files/zh-tw/web/javascript/data_structures/index.html b/files/zh-tw/web/javascript/data_structures/index.html
index f39aab97d0..a8462f6e55 100644
--- a/files/zh-tw/web/javascript/data_structures/index.html
+++ b/files/zh-tw/web/javascript/data_structures/index.html
@@ -11,7 +11,7 @@ translation_of: Web/JavaScript/Data_structures
 
 

JavaScript 是弱型別,也能說是動態的程式語言。這代表你不必特別宣告變數的型別。程式在運作時,型別會自動轉換。這也代表你可以以不同的型別使用同一個變數。

-
var foo = 42;    // foo 目前是數字
+
var foo = 42;    // foo 目前是數字
 var foo = 'bar'; // foo 目前是字串
 var foo = true;  // foo 目前是布林值
 
@@ -59,7 +59,7 @@ var foo = true; // foo 目前是布林值

The number type has only one integer that has two representations: 0 is represented as -0 and +0. ("0" is an alias for +0). In the praxis, this has almost no impact. For example +0 === -0 is true. However, you are able to notice this when you divide by zero:

-
> 42 / +0
+
> 42 / +0
 Infinity
 > 42 / -0
 -Infinity
diff --git a/files/zh-tw/web/javascript/guide/expressions_and_operators/index.html b/files/zh-tw/web/javascript/guide/expressions_and_operators/index.html
index 2792b5682a..c4a1dd74e6 100644
--- a/files/zh-tw/web/javascript/guide/expressions_and_operators/index.html
+++ b/files/zh-tw/web/javascript/guide/expressions_and_operators/index.html
@@ -28,19 +28,19 @@ translation_of: Web/JavaScript/Guide/Expressions_and_Operators
 
 

JavaScript 同時具有二元運算子及一元運算子, 以及一種特殊的 三元運算子,也就是 條件運算子。 一個二元運算子需要具備兩個運算元, 一個在運算元之前,一個在運算元之後:

-
運算元1 運算子 運算元2
+
運算元1 運算子 運算元2
 

例如, 3+4x*y.

一個 一元運算子 需要一個運算元, 位於運算子之前或之後:

-
運算子 運算元
+
運算子 運算元
 

-
運算元 運算子
+
運算元 運算子
 

例如, x++++x.

@@ -133,7 +133,7 @@ translation_of: Web/JavaScript/Guide/Expressions_and_Operators

為了進行更複雜的賦值,解構賦值是 JavaScript 用來從陣列或物件中提取資料的語法。

-
var foo = ['one', 'two', 'three'];
+
var foo = ['one', 'two', 'three'];
 
 // 不使用解構
 var one   = foo[0];
@@ -147,7 +147,7 @@ var [one, two, three] = foo;

比較運算子 會比較 運算元 並基於比較的結果回傳邏輯值。 運算元可以是數字,字串,邏輯,或物件的值。 字串的比較是基於字典序的, 使用 Unicode 的值。 在多數情況下,假如兩個運算元不具有相同型態, JavaScript 會嘗試將它們轉換成相同型態。這個行為通常是將運算元以數學形式對待。 在某些的轉換型態的例外中會使用到 ===!== 運算子, 它們會嚴格地進行相等或不相等的比較。 這些運算子不會在確認相等與否前嘗試轉換運算元的型態。 下面的表解釋了比較運算子:

-
var var1 = 3;
+
var var1 = 3;
 var var2 = 4;
 
@@ -220,7 +220,7 @@ var var2 = 4;

算術運算子 以 數值 (文字或變數也可以)作為其運算元,並回傳單一數值。最常見的算術運算元是 加法 (+),減法 (-), 乘法 (*),及除法 (/)。 這些運算子在大多數程式語言中功能相同 (比較特別的是,在除數為0時 {{jsxref("Infinity")}})。例如:

-
1 / 2; // 0.5
+
1 / 2; // 0.5
 1 / 2 == 1.0 / 2.0; // 會是 true
 
@@ -331,7 +331,7 @@ var var2 = 4;
  • 運算元被轉換為32 bits 的整數以二進位形式表示 (0 和 1)。大於 32 bits 的數字將被捨棄多出來的位元。例如, 下列整數大於32個bit但是會被轉換為32個bit的整數: -
    轉換之前:  11100110111110100000000000000110000000000001
    +  
    轉換之前:  11100110111110100000000000000110000000000001
     轉換之後:              10100000000000000110000000000001
  • 第一個運算元中的每個bit分別對應到第二個運算元的每個bit: 第一個 bit 對 第一個 bit, 第二個 bit 對 第二個 bit, 以此類推。
  • @@ -453,7 +453,7 @@ var var2 = 4;

    下面是 && (邏輯 AND) 運算子 的範例。

    -
    var a1 =  true && true;     // t && t 回傳 true
    +
    var a1 =  true && true;     // t && t 回傳 true
     var a2 =  true && false;    // t && f 回傳 false
     var a3 = false && true;     // f && t 回傳 false
     var a4 = false && (3 == 4); // f && f 回傳 false
    @@ -464,7 +464,7 @@ var a7 = "Cat" && false;    // t && f 回傳 false
     
     

    下列是 || (邏輯 OR) 運算子的範例。

    -
    var o1 =  true || true;     // t || t 回傳 true
    +
    var o1 =  true || true;     // t || t 回傳 true
     var o2 = false || true;     // f || t 回傳 true
     var o3 =  true || false;    // t || f 回傳 true
     var o4 = false || (3 == 4); // f || f 回傳 false
    @@ -475,7 +475,7 @@ var o7 = 'Cat' || false;    // t || f 回傳 Cat
     
     

    下列是 ! (邏輯 NOT) 運算子的範例。

    -
    var n1 = !true;  // !t 回傳 false
    +
    var n1 = !true;  // !t 回傳 false
     var n2 = !false; // !f 回傳 true
     var n3 = !'Cat'; // !t 回傳 false
     
    @@ -497,27 +497,27 @@ var n3 = !'Cat'; // !t 回傳 false

    例如,

    -
    console.log('我的 ' + '字串'); // 會印出 字串 "我的字串"。
    +
    console.log('我的 ' + '字串'); // 會印出 字串 "我的字串"。

    簡化的設定運算子 += 也能用於串接字串。

    例如,

    -
    var mystring = '字';
    +
    var mystring = '字';
     mystring += '母'; // 得到 "字母" 並賦與給變數 mystring.

    條件(三元)運算子

    條件運算子 是 JavaScript 中唯一需要三個運算元的運算子。 這個運算子接受兩個運算元作為值且一個運算元作為條件。 語法是:

    -
    條件 ? 值1 : 值2
    +
    條件 ? 值1 : 值2
     

    如果 條件 為 true,運算子回傳 值1, 否則回傳 值2。 你可以在任何使用標準運算子的地方改用 條件運算子。

    例如,

    -
    var status = (age >= 18) ? '成人' : '小孩';
    +
    var status = (age >= 18) ? '成人' : '小孩';
     

    這個陳述句會將 "成人" 賦與給變數 status 假如 age 大於等於18。 否則,會將 "小孩" 賦與給變數 status

    @@ -528,7 +528,7 @@ mystring += '母'; // 得到 "字母" 並賦與給變數 mystring.

    例如,假如 a 是一個有十個物件在裡面的二維陣列, 下面的程式中就使用了逗點運算子來同時更新兩個變數。 這段程式碼會印出陣列中所有對角線上的物件:

    -
    for (var i = 0, j = 9; i <= j; i++, j--)
    +
    for (var i = 0, j = 9; i <= j; i++, j--)
       console.log('a[' + i + '][' + j + ']= ' + a[i][j]);
     
    @@ -540,7 +540,7 @@ mystring += '母'; // 得到 "字母" 並賦與給變數 mystring.

    delete 運算子會刪除物件,物件的性質,或是陣列中指定 index 的物件。 語法是:

    -
    delete 物件名稱;
    +
    delete 物件名稱;
     delete 物件名稱.性質;
     delete 物件名稱[索引];
     delete 性質; // 只有在 with 陳述句中可以使用
    @@ -554,7 +554,7 @@ delete 性質; // 只有在 with 陳述句中可以使用
     
     

    假如 delete 運算子使用成功, 它會將物件 或是 物件的特性設定為 未定義。 delete 運算子會在運算成功時回傳 true ,失敗時回傳 false

    -
    x = 42;
    +
    x = 42;
     var y = 43;
     myobj = new Number();
     myobj.h = 4;    // 建立特性 h
    @@ -571,7 +571,7 @@ delete myobj;   // 回傳 true (在隱式宣告時可被刪除)
     
     

    當使用 delete 運算子刪除陣列中的一個元素後, 那個元素便不再存在於陣列中了。 在下面的程式中, trees[3] 被用 delete 移除了。然而, trees[3] 的記憶體位址仍可用並且會回傳 未定義。

    -
    var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
    +
    var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
     delete trees[3];
     if (3 in trees) {
       // 不會執行到這裡
    @@ -580,7 +580,7 @@ if (3 in trees) {
     
     

    假如你希望給予陣列元素 未定義 的值, 你可以直接使用 undefined 關鍵字而不是使用 delete 運算子。 下列範例中, trees[3] 被指定了 undefined, 然而陣列元素依然存在:

    -
    var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
    +
    var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
     trees[3] = undefined;
     if (3 in trees) {
       // 會執行這裡
    @@ -591,7 +591,7 @@ if (3 in trees) {
     
     

    typeof 運算子 能以下列任一方式使用:

    -
    typeof 運算元
    +
    typeof 運算元
     typeof (運算元)
     
    @@ -599,7 +599,7 @@ typeof (運算元)

    假設你定義了以下這些變數:

    -
    var myFun = new Function('5 + 2');
    +
    var myFun = new Function('5 + 2');
     var shape = 'round';
     var size = 1;
     var today = new Date();
    @@ -607,7 +607,7 @@ var today = new Date();
     
     

    typeof 運算子會回傳下列結果:

    -
    typeof myFun;       // 回傳 "function"
    +
    typeof myFun;       // 回傳 "function"
     typeof shape;       // 回傳 "string"
     typeof size;        // 回傳 "number"
     typeof today;       // 回傳 "object"
    @@ -616,26 +616,26 @@ typeof doesntExist; // 回傳 "undefined"
     
     

    對於 truenull關鍵字, typeof 運算子會回傳下列結果:

    -
    typeof true; // 回傳 "boolean"
    +
    typeof true; // 回傳 "boolean"
     typeof null; // 回傳 "object"
     

    對於字串或數字, typeof 運算子會回傳下列結果:

    -
    typeof 62;            // 回傳 "number"
    +
    typeof 62;            // 回傳 "number"
     typeof 'Hello world'; // 回傳 "string"
     

    對於特性,typeof 運算子會回傳 特性的值的類型:

    -
    typeof document.lastModified; // 回傳 "string"
    +
    typeof document.lastModified; // 回傳 "string"
     typeof window.length;         // 回傳 "number"
     typeof Math.LN2;              // 回傳 "number"
     

    對於 方法 及 函式, typeof 運算子會回傳下列結果:

    -
    typeof blur;        // 回傳 "function"
    +
    typeof blur;        // 回傳 "function"
     typeof eval;        // 回傳 "function"
     typeof parseInt;    // 回傳 "function"
     typeof shape.split; // 回傳 "function"
    @@ -643,7 +643,7 @@ typeof shape.split; // 回傳 "function"
     
     

    對於內建定義的物件, typeof 運算子會回傳下列結果:

    -
    typeof Date;     // 回傳 "function"
    +
    typeof Date;     // 回傳 "function"
     typeof Function; // 回傳 "function"
     typeof Math;     // 回傳 "object"
     typeof Option;   // 回傳 "function"
    @@ -654,7 +654,7 @@ typeof String;   // 回傳 "function"
     
     

    void 運算子 能以下列任一方式使用:

    -
    void (運算式)
    +
    void (運算式)
     void 運算式
     
    @@ -664,12 +664,12 @@ void 運算式

    下列範例是一個在點擊時甚麼都不做的超連結。 當使用者點擊連結時, void(0) 被解析為 未定義, 而甚麼都不會發生。

    -
    <a href="javascript:void(0)">點擊這裡,甚麼都不會發生</a>
    +
    <a href="javascript:void(0)">點擊這裡,甚麼都不會發生</a>
     

    下列範例是一個在使用者點擊時傳送表單的超連結。

    -
    <a href="javascript:void(document.form.submit())">
    +
    <a href="javascript:void(document.form.submit())">
     點擊以送出</a>

    關係運算子

    @@ -680,14 +680,14 @@ void 運算式

    in 運算子 在指定性質存在於物件中時回傳 true 。 語法是:

    -
    性質名稱 in 物件名稱
    +
    性質名稱 in 物件名稱
     

    性質名稱 可以是 字串或數字,或是陣列的索引, 且 物件名稱 是物件的名稱。

    下列範例示範了 in 運算子的一些用法。

    -
    // 陣列
    +
    // 陣列
     var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
     0 in trees;        // 回傳 true
     3 in trees;        // 回傳 true
    @@ -711,7 +711,7 @@ var mycar = { make: 'Honda', model: 'Accord', year: 1998 };
     
     

    instanceof 運算子 在 指定物件 具有 指定的物件型態 時回傳 true。 語法是:

    -
    物件名稱 instanceof 物件類型
    +
    物件名稱 instanceof 物件類型
     

    物件名稱 是用來與 物件類型 比較的物件的名字, 物件類型 是物件的類型, 例如 {{jsxref("Date")}} 或 {{jsxref("Array")}}。

    @@ -720,7 +720,7 @@ var mycar = { make: 'Honda', model: 'Accord', year: 1998 };

    例如,下列程式碼使用 instanceof 來判斷變數 theDay 是不是 Date 類型的物件。 因為 theDayDate 類型的物件, 所以if 陳述中的陳述句會被執行。

    -
    var theDay = new Date(1995, 12, 17);
    +
    var theDay = new Date(1995, 12, 17);
     if (theDay instanceof Date) {
       // 會被執行的陳述
     }
    @@ -838,13 +838,13 @@ if (theDay instanceof Date) {
     
     

    this 關鍵字 能取得當前所在物件。 一般而言, this 能取得呼叫處所在的物件。 你可以使用 點 或是 中括號 來取用該物件中的特性:

    -
    this['特性名稱']
    +
    this['特性名稱']
     this.特性名稱
     

    以下定義一個叫做 validate 的函式,比較物件中特性 value 與傳入的兩變數:

    -
    function validate(obj, lowval, hival){
    +
    function validate(obj, lowval, hival){
       if ((obj.value < lowval) || (obj.value > hival))
         console.log('不可用的值!');
     }
    @@ -852,7 +852,7 @@ this.特性名稱
     
     

    你可以在表單的 onChange event handler 中呼叫 validate 函式, 並以 this 來傳入表單的元素, 範例如下:

    -
    <p>請輸入一介於18 與 99 的數字:</p>
    +
    <p>請輸入一介於18 與 99 的數字:</p>
     <input type="text" name="age" size=3 onChange="validate(this, 18, 99);">
     
    @@ -860,7 +860,7 @@ this.特性名稱

    分組運算子 ( ) 控制了運算子的優先順序。 例如,你可以覆寫先乘除,後加減的優先順序,使其變成先加減,後乘除。

    -
    var a = 1;
    +
    var a = 1;
     var b = 2;
     var c = 3;
     
    @@ -890,7 +890,7 @@ a * c + b * c // 9
     
     

    解析在許多程式語言中都存在,允許你快速地基於現存陣列產生新的陣列,例如:

    -
    [for (i of [ 1, 2, 3 ]) i*i ];
    +
    [for (i of [ 1, 2, 3 ]) i*i ];
     // [ 1, 4, 9 ]
     
     var abc = [ 'A', 'B', 'C' ];
    @@ -905,14 +905,14 @@ var abc = [ 'A', 'B', 'C' ];
     
     

    你可以使用 new 運算子 來建立一個使用者自定義物件或內建物件的實例。 用法如下:

    -
    var 物件名稱 = new 物件型態([參數1, 參數2, ..., 參數N]);
    +
    var 物件名稱 = new 物件型態([參數1, 參數2, ..., 參數N]);
     

    super

    super 關鍵字 用於呼叫物件的父物件中的函式。 在使用 類別 來呼叫父類別的建構子時很實用,例如:

    -
    super([參數]); // 呼叫父物件的建構子.
    +
    super([參數]); // 呼叫父物件的建構子.
     super.父物件的函式([參數]);
     
    @@ -922,12 +922,12 @@ super.父物件的函式([參數]);

    範例: 現在你想要用已存在的一個陣列做為新的一個陣列的一部份,當字串常數不再可用而你必須使用指令式編程,也就是使用,一連串的 push, splice, concat,等等。 展開運算子能讓過程變得更加簡潔:

    -
    var parts = ['肩膀', '膝蓋'];
    +
    var parts = ['肩膀', '膝蓋'];
     var lyrics = ['頭', ...parts, '和', '腳趾'];

    相同的,展開運算子也適用於函式呼叫:

    -
    function f(x, y, z) { }
    +
    function f(x, y, z) { }
     var args = [0, 1, 2];
     f(...參數);
    diff --git a/files/zh-tw/web/javascript/guide/numbers_and_dates/index.html b/files/zh-tw/web/javascript/guide/numbers_and_dates/index.html index 88ed75fd3e..4bc86711c0 100644 --- a/files/zh-tw/web/javascript/guide/numbers_and_dates/index.html +++ b/files/zh-tw/web/javascript/guide/numbers_and_dates/index.html @@ -21,7 +21,7 @@ translation_of: Web/JavaScript/Guide/Numbers_and_dates

    十進制數值

    -
    1234567890
    +
    1234567890
     42
     
     // 以零為開頭時要小心:
    @@ -36,7 +36,7 @@ translation_of: Web/JavaScript/Guide/Numbers_and_dates
     
     

    二進制數值以 0 為開頭並跟著一個大寫或小寫的英文字母 「B」 (0b 或 0B)。如果 0b 後面接著的數字不是 0 或 1,那會丟出 SyntaxError(語法錯誤): "Missing binary digits after 0b"。

    -
    var FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
    +
    var FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
     var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
     var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607
    @@ -44,27 +44,27 @@ var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607

    八進制數值以 0 為開頭。如果 0 後面的數字超出 0 到 7 這個範圍,將會被解析成十進制數值。

    -
    var n = 0755; // 493
    +
    var n = 0755; // 493
     var m = 0644; // 420
     

    Strict mode in ECMAScript 5 forbids octal syntax. Octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero: 0644 === 420 and"\045" === "%". In ECMAScript 2015, octal numbers are supported if they are prefixed with 0o, e.g.: 

    -
    var a = 0o10; // ES2015: 8
    +
    var a = 0o10; // ES2015: 8
     

    十六進制數值

    十六進制數值以 0 為開頭並跟著一個大寫或小寫的英文字母 「X」(0x 或 0X)。如果 0b 後面接著的值超出範圍 (0123456789ABCDEF),那會丟出 SyntaxError(語法錯誤):"Identifier starts immediately after numeric literal"。

    -
    0xFFFFFFFFFFFFFFFFF // 295147905179352830000
    +
    0xFFFFFFFFFFFFFFFFF // 295147905179352830000
     0x123456789ABCDEF   // 81985529216486900
     0XA                 // 10
     

    指數運算

    -
    1E3   // 1000
    +
    1E3   // 1000
     2e6   // 2000000
     0.1e2 // 10
    @@ -72,7 +72,7 @@ var m = 0644; // 420

    The built-in {{jsxref("Number")}} object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:

    -
    var biggestNum = Number.MAX_VALUE;
    +
    var biggestNum = Number.MAX_VALUE;
     var smallestNum = Number.MIN_VALUE;
     var infiniteNum = Number.POSITIVE_INFINITY;
     var negInfiniteNum = Number.NEGATIVE_INFINITY;
    @@ -196,12 +196,12 @@ var notANum = Number.NaN;
     
     

    The built-in {{jsxref("Math")}} object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi (3.141...), which you would use in an application as

    -
    Math.PI
    +
    Math.PI
     

    Similarly, standard mathematical functions are methods of Math. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write

    -
    Math.sin(1.56)
    +
    Math.sin(1.56)
     

    Note that all trigonometric methods of Math take arguments in radians.

    @@ -288,7 +288,7 @@ var notANum = Number.NaN;

    創建一個Date物件:

    -
    var dateObjectName = new Date([parameters]);
    +
    var dateObjectName = new Date([parameters]);
     

    在這裡創建一個名為dateObjectNameDate 物件;它可以是一個新的物件或是某個以存在的物件當中的屬性。

    @@ -328,7 +328,7 @@ var notANum = Number.NaN;

    舉例,假設你定義了一個日期如下:

    -
    var Xmas95 = new Date('December 25, 1995');
    +
    var Xmas95 = new Date('December 25, 1995');
     

    那 Xmas95.getMonth() 將會回傳 11, Xmas95.getFullYear() 會回傳 1995。

    @@ -337,7 +337,7 @@ var notANum = Number.NaN;

    For example, the following code displays the number of days left in the current year:

    -
    var today = new Date();
    +
    var today = new Date();
     var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // Set day and month
     endYear.setFullYear(today.getFullYear()); // Set year to this year
     var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
    @@ -349,7 +349,7 @@ var daysLeft = Math.round(daysLeft); //returns days left in the year
     
     

    The parse method is useful for assigning values from date strings to existing Date objects. For example, the following code uses parse and setTime to assign a date value to the IPOdate object:

    -
    var IPOdate = new Date();
    +
    var IPOdate = new Date();
     IPOdate.setTime(Date.parse('Aug 9, 1995'));
     
    @@ -357,7 +357,7 @@ IPOdate.setTime(Date.parse('Aug 9, 1995'));

    下面這個範例,JSClock() 這個函式將會以數位時鐘的格式回傳時間。

    -
    function JSClock() {
    +
    function JSClock() {
       var time = new Date();
       var hour = time.getHours();
       var minute = time.getMinutes();
    diff --git a/files/zh-tw/web/javascript/inheritance_and_the_prototype_chain/index.html b/files/zh-tw/web/javascript/inheritance_and_the_prototype_chain/index.html
    index 12ae7cee8d..744f6d9575 100644
    --- a/files/zh-tw/web/javascript/inheritance_and_the_prototype_chain/index.html
    +++ b/files/zh-tw/web/javascript/inheritance_and_the_prototype_chain/index.html
    @@ -27,7 +27,7 @@ translation_of: Web/JavaScript/Inheritance_and_the_prototype_chain
     
     

    以下是嘗試存取屬性時會發生的事:

    -
    // 利用含有 a 與 b 屬性的 f 函式,建立一個 o 物件:
    +
    // 利用含有 a 與 b 屬性的 f 函式,建立一個 o 物件:
     let f = function () {
        this.a = 1;
        this.b = 2;
    @@ -74,7 +74,7 @@ console.log(o.d); // undefined
     
     

    當繼承函式執行時,this 值指向繼承的物件,而不是在函式內擁有屬性的原型物件。

    -
    var o = {
    +
    var o = {
       a: 2,
       m: function() {
         return this.a + 1;
    @@ -98,7 +98,7 @@ console.log(p.m()); // 5
     
     

    含有語法結構的物件

    -
    var o = {a: 1};
    +
    var o = {a: 1};
     
     // 新建的 o 有個自己的 [[Prototype]] 稱為 Object.prototype
     // o 自己並沒有稱為「hasOwnProperty」的屬性
    @@ -125,7 +125,7 @@ function f() {
     
     

    JavaScript 建構子,就、只、是、個、被 new 運算子呼叫的函式。

    -
    function Graph() {
    +
    function Graph() {
       this.vertices = [];
       this.edges = [];
     }
    @@ -145,7 +145,7 @@ var g = new Graph();
     
     

    ECMAScript 5 引入了新方法:{{jsxref("Object.create()")}}。呼叫這個方法就可以建立新的物件。這個物件的原型,為函式的第一個參數。

    -
    var a = {a: 1};
    +
    var a = {a: 1};
     // a ---> Object.prototype ---> null
     
     var b = Object.create(a);
    @@ -165,7 +165,7 @@ console.log(d.hasOwnProperty);
     
     

    ECMAScript 2015 引入了新的類別實做。儘管對那些基於類別的開發者來說,這種結構體令他們感到熟悉,它們依舊不一樣。JavaScript 依舊是基於原型的。新的關鍵字包括 {{jsxref("Statements/class", "class")}}、{{jsxref("Classes/constructor", "constructor")}}、{{jsxref("Classes/static", "static")}}、{{jsxref("Classes/extends", "extends")}}、{{jsxref("Operators/super", "super")}}。

    -
    'use strict';
    +
    'use strict';
     
     class Polygon {
       constructor(height, width) {
    @@ -198,7 +198,7 @@ var square = new Square(2);
     
     

    要檢查物件本身有沒有指定的屬性、也不需要查找整個原型鏈時,你必須使用由 Object.prototype 繼承的 hasOwnProperty 方法。

    -
    console.log(g.hasOwnProperty('vertices'));
    +
    console.log(g.hasOwnProperty('vertices'));
     // true
     
     console.log(g.hasOwnProperty('nope'));
    @@ -227,7 +227,7 @@ console.log(g.__proto__.hasOwnProperty('addVertex'));
     
     

    B 要繼承自 A

    -
    function A(a) {
    +
    function A(a) {
       this.varA = a;
     }
     
    @@ -291,17 +291,17 @@ b.doSomething();
     
     

    因此當你:

    -
    var o = new Foo();
    +
    var o = new Foo();

    JavaScript 其實會:

    -
    var o = new Object();
    +
    var o = new Object();
     o.[[Prototype]] = Foo.prototype;
     Foo.call(o);

    或偶爾這樣:

    -
    o.someProp;
    +
    o.someProp;

    時,它檢查了 o 有沒有 someProp 屬性。如果沒有,就檢查 Object.getPrototypeOf(o).someProp;再沒有就檢查 Object.getPrototypeOf(Object.getPrototypeOf(o)).someProp,依此類推。

    diff --git a/files/zh-tw/web/javascript/memory_management/index.html b/files/zh-tw/web/javascript/memory_management/index.html index d63cb223ff..16376c4d40 100644 --- a/files/zh-tw/web/javascript/memory_management/index.html +++ b/files/zh-tw/web/javascript/memory_management/index.html @@ -31,7 +31,7 @@ translation_of: Web/JavaScript/Memory_Management

    為了不讓開發者對配置感到困擾,JavaScript 會在宣告值的同時完成記憶體配置

    -
    var n = 123; // 配置一記憶體空間給數字
    +
    var n = 123; // 配置一記憶體空間給數字
     var s = 'azerty'; // 配置記憶體空間給字串
     
     var o = {
    @@ -57,13 +57,13 @@ someElement.addEventListener('click', function() {
     
     

    有些函式呼叫後產生物件配置。

    -
    var d = new Date(); // 配置一個日期物件
    +
    var d = new Date(); // 配置一個日期物件
     
     var e = document.createElement('div'); // 配置一個 DOM 物件

    有些方法配置新的值或物件:

    -
    var s = 'azerty';
    +
    var s = 'azerty';
     var s2 = s.substr(0, 3); // s2 是一個新字串
     // 因為字串是一種不可變的值,
     // JavaScript 會決定不分配新的記憶體,
    @@ -104,7 +104,7 @@ var a3 = a.concat(a2);
     
     

    範例

    -
    var o = {
    +
    var o = {
       a: {
         b: 2
       }
    @@ -135,7 +135,7 @@ oa = null; // 現在把 oa 變成 null
     
     

    當涉及到循環時有一個限制。在下面的例子中,創造兩個物件並相互參考,從而製作一個循環。當呼叫函式時,他們應該超出作用範圍,因此他們實際上是無用且可釋放。但垃圾回收參考計數演算法會認為,兩個物件都至少被參考一次,因此兩個都是不能被回收的。

    -
    function f() {
    +
    function f() {
       var o = {};
       var o2 = {};
       o.a = o2; // o 參考 o2
    @@ -151,7 +151,7 @@ f();
     
     

    Internet Explorer 6和7有一個DOM物件的垃圾回收參考計數。循環是一個造成記憶體洩漏(memory leaks)的常見問題:

    -
    var div;
    +
    var div;
     window.onload = function() {
       div = document.getElementById('myDivElement');
       div.circularReference = div;
    @@ -193,7 +193,7 @@ window.onload = function() {
     
     

    We can also expose the garbage collector for debugging memory issues using a flag and the Chrome Debugger:

    -
    node --expose-gc --inspect index.js
    +
    node --expose-gc --inspect index.js

    可以參考

    diff --git a/files/zh-tw/web/javascript/reference/classes/constructor/index.html b/files/zh-tw/web/javascript/reference/classes/constructor/index.html index 8da81e9b71..aa0e2bf896 100644 --- a/files/zh-tw/web/javascript/reference/classes/constructor/index.html +++ b/files/zh-tw/web/javascript/reference/classes/constructor/index.html @@ -9,7 +9,7 @@ translation_of: Web/JavaScript/Reference/Classes/constructor

    語法

    -
    constructor([arguments]) { ... }
    +
    constructor([arguments]) { ... }

    敘述

    @@ -23,7 +23,7 @@ translation_of: Web/JavaScript/Reference/Classes/constructor

    這段程式碼是從 classes sample 擷取而來。(線上範例

    -
    class Square extends Polygon {
    +
    class Square extends Polygon {
       constructor(length) {
         // 我們在這裡呼叫了 class 的建構子提供多邊形的長寬值
         super(length, length);
    @@ -44,12 +44,12 @@ translation_of: Web/JavaScript/Reference/Classes/constructor
     
     

    如上文所說:如果不指定建構子,就會使用預設的建構子。對 base classes 而言,預設的建構子長得像這樣:

    -
    constructor() {}
    +
    constructor() {}
     

    對 derived class 而言,預設的建構子長得像這樣:

    -
    constructor(...args) {
    +
    constructor(...args) {
       super(...args);
     }
    diff --git a/files/zh-tw/web/javascript/reference/functions/arguments/index.html b/files/zh-tw/web/javascript/reference/functions/arguments/index.html index 6b1d6a45a1..ddccefae10 100644 --- a/files/zh-tw/web/javascript/reference/functions/arguments/index.html +++ b/files/zh-tw/web/javascript/reference/functions/arguments/index.html @@ -16,7 +16,7 @@ translation_of: Web/JavaScript/Reference/Functions/arguments

    語法

    -
    arguments
    +
    arguments

    描述

    @@ -32,20 +32,20 @@ translation_of: Web/JavaScript/Reference/Functions/arguments

    For example, if a function is passed three arguments, you can refer to them as follows:

    -
    arguments[0]
    +
    arguments[0]
     arguments[1]
     arguments[2]
     

    arguments 也可以被指定:

    -
    arguments[1] = 'new value';
    +
    arguments[1] = 'new value';

    arguments 物件不是陣列。它與陣列非常相似,但是它沒有除了 length 這個屬性以外的其他陣列屬性。舉例,它沒有 pop 這個陣列方法。

    然而,它依然可以被轉換為真正的陣列(Array)。

    -
    var args = Array.prototype.slice.call(arguments);
    +
    var args = Array.prototype.slice.call(arguments);
     var args = [].slice.call(arguments);
     
     // ES2015
    @@ -55,7 +55,7 @@ const args = Array.from(arguments);
     

    Using slice on arguments prevents optimizations in some JavaScript engines (V8 for example - more information). If you care for them, try constructing a new array by iterating through the arguments object instead. An alternative would be to use the despised Array constructor as a function:

    -
    var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
    +
    var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));

    You can use the arguments object if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments. Use arguments.length to determine the number of arguments passed to the function, and then process each argument by using the arguments object. To determine the number of parameters in the function signature, use the Function.length property.

    @@ -64,17 +64,17 @@ const args = Array.from(arguments);

    The typeof arguments returns 'object'. 

    -
    console.log(typeof arguments); // 'object' 
    +
    console.log(typeof arguments); // 'object' 

    The typeof individual arguments can be determined with the use of indexing.

    -
    console.log(typeof arguments[0]); //this will return the typeof individual arguments.
    +
    console.log(typeof arguments[0]); //this will return the typeof individual arguments.

    Using the Spread Syntax with Arguments

    You can also use the {{jsxref("Array.from()")}} method or the spread operator to convert arguments to a real Array:

    -
    var args = Array.from(arguments);
    +
    var args = Array.from(arguments);
     var args = [...arguments];
     
    @@ -97,14 +97,14 @@ var args = [...arguments];

    This example defines a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:

    -
    function myConcat(separator) {
    +
    function myConcat(separator) {
       var args = Array.prototype.slice.call(arguments, 1);
       return args.join(separator);
     }

    You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.

    -
    // returns "red, orange, blue"
    +
    // returns "red, orange, blue"
     myConcat(', ', 'red', 'orange', 'blue');
     
     // returns "elephant; giraffe; lion; cheetah"
    @@ -117,7 +117,7 @@ myConcat('. ', 'sage', 'basil', 'oregano', 'pepper', 'parsley');

    This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "u" if the list is to be unordered (bulleted), or "o" if the list is to be ordered (numbered). The function is defined as follows:

    -
    function list(type) {
    +
    function list(type) {
       var result = '<' + type + 'l><li>';
       var args = Array.prototype.slice.call(arguments, 1);
       result += args.join('</li><li>');
    @@ -128,7 +128,7 @@ myConcat('. ', 'sage', 'basil', 'oregano', 'pepper', 'parsley');

    You can pass any number of arguments to this function, and it adds each argument as an item to a list of the type indicated. For example:

    -
    var listHTML = list('u', 'One', 'Two', 'Three');
    +
    var listHTML = list('u', 'One', 'Two', 'Three');
     
     /* listHTML is:
     
    @@ -140,7 +140,7 @@ myConcat('. ', 'sage', 'basil', 'oregano', 'pepper', 'parsley');

    The arguments object can be used in conjunction with rest, default, and destructured parameters.

    -
    function foo(...args) {
    +
    function foo(...args) {
       return args;
     }
     foo(1, 2, 3); // [1,2,3]
    @@ -150,7 +150,7 @@ foo(1, 2, 3); // [1,2,3]
     
     

    When a non-strict function does not contain restdefault, or destructured parameters, then the values in the arguments object do track the values of the arguments (and vice versa). See the code below:

    -
    function func(a) {
    +
    function func(a) {
       arguments[0] = 99; // updating arguments[0] also updates a
       console.log(a);
     }
    @@ -159,7 +159,7 @@ func(10); // 99
     
     

    and

    -
    function func(a) {
    +
    function func(a) {
       a = 99; // updating a also updates arguments[0]
       console.log(arguments[0]);
     }
    @@ -168,7 +168,7 @@ func(10); // 99
     
     

    When a non-strict function does contain restdefault, or destructured parameters, then the values in the arguments object do not track the values of the arguments (and vice versa). Instead, they reflect the arguments provided at the time of invocation:

    -
    function func(a = 55) {
    +
    function func(a = 55) {
       arguments[0] = 99; // updating arguments[0] does not also update a
       console.log(a);
     }
    @@ -176,7 +176,7 @@ func(10); // 10

    and

    -
    function func(a = 55) {
    +
    function func(a = 55) {
       a = 99; // updating a does not also update arguments[0]
       console.log(arguments[0]);
     }
    @@ -185,7 +185,7 @@ func(10); // 10
     
     

    and

    -
    function func(a = 55) {
    +
    function func(a = 55) {
       console.log(arguments[0]);
     }
     func(); // undefined
    diff --git a/files/zh-tw/web/javascript/reference/functions/arrow_functions/index.html b/files/zh-tw/web/javascript/reference/functions/arrow_functions/index.html index 82285afeae..0b53f14444 100644 --- a/files/zh-tw/web/javascript/reference/functions/arrow_functions/index.html +++ b/files/zh-tw/web/javascript/reference/functions/arrow_functions/index.html @@ -11,7 +11,7 @@ translation_of: Web/JavaScript/Reference/Functions/Arrow_functions

    基本語法

    -
    (參數1, 參數2, …, 參數N) => { 陳述式; }
    +
    (參數1, 參數2, …, 參數N) => { 陳述式; }
     
     (參數1, 參數2, …, 參數N) => 表示式;
     // 等相同(參數1, 參數2, …, 參數N) => { return 表示式; }
    @@ -26,7 +26,7 @@ translation_of: Web/JavaScript/Reference/Functions/Arrow_functions
     
     

    進階語法

    -
    // 用大括號將內容括起來,返回一個物件字面值表示法:
    +
    // 用大括號將內容括起來,返回一個物件字面值表示法:
     params => ({foo: bar})
     
     // 支援其餘參數預設參數
    @@ -46,7 +46,7 @@ var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c; f(); // 6
     
     

    更短的函式寫法

    -
    var elements = [
    +
    var elements = [
       'Hydrogen',
       'Helium',
       'Lithium',
    @@ -96,7 +96,7 @@ elements.map(({ length }) => length); // [8, 6, 7, 9]
     
     

    事實證明這對物件導向程式設計來說並不理想。

    -
    function Person() {
    +
    function Person() {
       // Person() 建構式將 this 定義為它自己的一個實體
       this.age = 0;
     
    @@ -112,7 +112,7 @@ var p = new Person();

    在 ECMAScript 3/5 裡面,有關 this 的問題,可以透過指派 this 值給可以關閉的變數解決。

    -
    function Person() {
    +
    function Person() {
       var self = this; // 有些人喜歡 `that` 而不是 `self`.
                        // 選好一種取法後始終如一
       self.age = 0;
    @@ -129,7 +129,7 @@ var p = new Person();

    因此,在以下程式碼內,傳遞給 setInterval 的 箭頭函式中的this ,會與封閉函式的 this 值相同:

    -
    function Person(){
    +
    function Person(){
       this.age = 0;
     
       setInterval(() => {
    @@ -143,7 +143,7 @@ var p = new Person();

    由於 this 變數具有詞彙上綁定意義,所以嚴格模式的宣告對 this 的作用將被忽略。

    -
    var f = () => {'use strict'; return this};
    +
    var f = () => {'use strict'; return this};
     f() === window; // 或是 global 物件

    但嚴格模式的其他作用依舊存在。

    @@ -152,7 +152,7 @@ f() === window; // 或是 global 物件

    由於箭頭函式並沒有自己的 this,所以透過 call()apply() 呼叫箭頭函式只能傳入參數。thisArg 將會被忽略。

    -
    var adder = {
    +
    var adder = {
       base : 1,
       add : function(a) {
         var f = v => v + this.base;
    @@ -174,7 +174,7 @@ console.log(adder.addThruCall(1)); // 依舊顯示 2
     
     

    箭頭函式並沒有自己的 arguments 物件。所以在此例中,arguments 只是參考到 enclosing 作用域裡面的相同變數:

    -
    var arguments = [1, 2, 3];
    +
    var arguments = [1, 2, 3];
     var arr = () => arguments[0];
     
     arr(); // 1
    @@ -188,7 +188,7 @@ foo(1); // 2

    大多時候,使用其餘參數 是取代 arguments 物件的較好方式。

    -
    function foo(n) {
    +
    function foo(n) {
       var f = (...args) => args[0] + n;
       return f(10);
     }
    @@ -199,7 +199,7 @@ foo(1); // 11

    如同前述,箭頭函式運算式最適合用在非方法的函式。來看看如果要把它們當成方法來用,會發生什麼事:

    -
    'use strict';
    +
    'use strict';
     var obj = {
       i: 10,
       b: () => console.log(this.i, this),
    @@ -212,7 +212,7 @@ obj.c(); // 印出 10, Object {...}

    箭頭函式並沒有自己的 this。另一個例子與 {{jsxref("Object.defineProperty()")}} 有關:

    -
    'use strict';
    +
    'use strict';
     
     var obj = {
       a: 10
    @@ -230,14 +230,14 @@ Object.defineProperty(obj, 'b', {
     
     

    箭頭函式不可作為建構式使用;若使用於建構式,會在使用 new 時候拋出錯誤。

    -
    var Foo = () => {};
    +
    var Foo = () => {};
     var foo = new Foo(); // TypeError: Foo is not a constructor

    使用 prototype 屬性

    箭頭函式並沒有原型(prototype)屬性。

    -
    var Foo = () => {};
    +
    var Foo = () => {};
     console.log(Foo.prototype); // undefined
     
    @@ -251,21 +251,21 @@ console.log(Foo.prototype); // undefined

    在 concise body 裡面只需要輸入運算式,就會附上內建的回傳。在 block body 裡面就必須附上明確的 return 宣告。

    -
    var func = x => x * x;                  // concise 語法會內建 "return"
    +
    var func = x => x * x;                  // concise 語法會內建 "return"
     var func = (x, y) => { return x + y; }; // block body 需要明確的 "return"

    回傳物件字面值

    請注意只使用 params => {object:literal} 並不會按照期望般回傳物件字面值(object literal)。

    -
    var func = () => { foo: 1 };               // Calling func() returns undefined!
    +
    var func = () => { foo: 1 };               // Calling func() returns undefined!
     var func = () => { foo: function() {} };   // SyntaxError: Unexpected token (

    因為在大括弧({})裡面的文字會被解析為有序宣告(例如 foo 會被當作標記(label)、而不是物件的 key )

    要記得把物件字面值包在圓括弧內。

    -
    var func = () => ({foo: 1});
    +
    var func = () => ({foo: 1});
     var func = () => ({ foo: function() {} }); 
     
    @@ -273,14 +273,14 @@ var func = () => ({ foo: function() {} });

    箭頭函式不可以在參數及箭頭間包含換行。

    -
    var func = ()
    +
    var func = ()
                => 1; // SyntaxError: expected expression, got '=>'

    Parsing order

    箭頭函式的箭頭儘管不是操作符,但藉著運算子優先等級,箭頭函式有著和普通函式不相同的特殊解析規則。

    -
    let callback;
    +
    let callback;
     
     callback = callback || function() {}; // ok
     callback = callback || () => {};      // SyntaxError: invalid arrow-function arguments
    @@ -288,7 +288,7 @@ callback = callback || (() => {});    // ok

    更多範例

    -
    // 回傳 undefined 的箭頭函式
    +
    // 回傳 undefined 的箭頭函式
     let empty = () => {};
     
     (() => "foobar")() // 回傳 "foobar"
    diff --git a/files/zh-tw/web/javascript/reference/functions/default_parameters/index.html b/files/zh-tw/web/javascript/reference/functions/default_parameters/index.html
    index 6faacba9a3..e7a796f4d1 100644
    --- a/files/zh-tw/web/javascript/reference/functions/default_parameters/index.html
    +++ b/files/zh-tw/web/javascript/reference/functions/default_parameters/index.html
    @@ -9,7 +9,7 @@ translation_of: Web/JavaScript/Reference/Functions/Default_parameters
     
     

    語法

    -
    function [name]([param1[ = defaultValue1 ][, ..., paramN[ = defaultValueN ]]]) {
    +
    function [name]([param1[ = defaultValue1 ][, ..., paramN[ = defaultValueN ]]]) {
        要執行的程序
     }
     
    @@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Functions/Default_parameters

    以往設定預設值有個普遍方法:在函式的內容裡檢查傳入參數是否為 undefined ,如果是的話,爲他指定一個值。如下列範例,若函式被呼叫時,並沒有提供 b 的值,它的值就會是 undefined,在計算 a*b 時,以及呼叫 multiply 時,就會回傳 NaN。然而這在範例的第二行被阻止了::

    -
    function multiply(a, b) {
    +
    function multiply(a, b) {
       b = (typeof b !== 'undefined') ?  b : 1;
       return a * b;
     }
    @@ -32,7 +32,7 @@ multiply(5);    // 5
     
     

    有了 ES2015 的預設參數,再也不用於函式進行檢查了,現在只要簡單的在函式的起始處為 b 指定 1 的值:

    -
    function multiply(a, b = 1) {
    +
    function multiply(a, b = 1) {
       return a * b;
     }
     
    @@ -47,7 +47,7 @@ multiply(5);    // 5
     
     

    這邊第二段函式呼叫中,僅管第二個傳入參數在呼叫時明確地指定為undefined(雖不是null),其顏色參數的值是預設值(rosybrown)。

    -
    function setBackgroundColor(element, color = 'rosybrown') {
    +
    function setBackgroundColor(element, color = 'rosybrown') {
       element.style.backgroundColor = color;
     }
     
    @@ -60,7 +60,7 @@ setBackgroundColor(someDiv, 'blue');    // color set to 'blue'
     
     

    跟Python等語言不同的地方是,先前預設的代數值會拿來進行函式內的程序,也因此在函式呼叫的時候,會建立新物件。

    -
    function append(value, array = []) {
    +
    function append(value, array = []) {
       array.push(value);
       return array;
     }
    @@ -71,7 +71,7 @@ append(2); //[2], 而非 [1, 2]
     
     

    諸如此類的做法,也適用在函式和變量。

    -
    function callSomething(thing = something()) {
    +
    function callSomething(thing = something()) {
      return thing;
     }
     
    @@ -85,7 +85,7 @@ callSomething();  //sth

    先前有碰到的參數,後來的即可使用。

    -
    function singularAutoPlural(singular, plural = singular + '們',
    +
    function singularAutoPlural(singular, plural = singular + '們',
                                 rallyingCry = plural + ',進攻啊!!!') {
       return [singular, plural, rallyingCry];
     }
    @@ -103,7 +103,7 @@ singularAutoPlural('鹿兒', '鹿兒們', '鹿兒們平心靜氣的 \
     
     

    This functionality is approximated in a straight forward fashion and demonstrates how many edge cases are handled.

    -
    function go() {
    +
    function go() {
       return ':P';
     }
     
    @@ -145,7 +145,7 @@ withoutDefaults.call({value: '=^_^='});
     
     

    Introduced in Gecko 33 {{geckoRelease(33)}}. Functions declared in the function body cannot be referred inside default parameters and throw a {{jsxref("ReferenceError")}} (currently a {{jsxref("TypeError")}} in SpiderMonkey, see {{bug(1022967)}}). Default parameters are always executed first, function declarations inside the function body evaluate afterwards.

    -
    // 行不通的! 最後會丟出 ReferenceError。
    +
    // 行不通的! 最後會丟出 ReferenceError。
     function f(a = go()) {
       function go() { return ':P'; }
     }
    @@ -155,7 +155,7 @@ function f(a = go()) {
     
     

    Prior to Gecko 26 {{geckoRelease(26)}}, the following code resulted in a {{jsxref("SyntaxError")}}. This has been fixed in {{bug(777060)}} and works as expected in later versions. Parameters are still set left-to-right, overwriting default parameters even if there are later parameters without defaults.

    -
    function f(x = 1, y) {
    +
    function f(x = 1, y) {
       return [x, y];
     }
     
    @@ -167,7 +167,7 @@ f(2); // [2, undefined]
     
     

    You can use default value assignment with the destructuring assignment notation:

    -
    function f([x, y] = [1, 2], {z: z} = {z: 3}) {
    +
    function f([x, y] = [1, 2], {z: z} = {z: 3}) {
       return x + y + z;
     }
     
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/length/index.html b/files/zh-tw/web/javascript/reference/global_objects/array/length/index.html
    index 453564d528..1b6497b07a 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/array/length/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/array/length/index.html
    @@ -7,7 +7,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/length
     
     

    length 為Array物件的屬性 ,可供設定或回傳該陣列實體中包含的元素個數。其值必為一大於零、32位元、且恆大於該陣列最大索引數的正整數。

    -
    var items = ['shoes', 'shirts', 'socks', 'sweaters'];
    +
    var items = ['shoes', 'shirts', 'socks', 'sweaters'];
     items.length;
     
     // returns 4
    @@ -16,7 +16,7 @@ items.length;

    length 屬性的值必為一正整數,其值必介於 0 ~ 232 (不包含)之間.

    -
    var namelistA = new Array(4294967296); //232 = 4294967296
    +
    var namelistA = new Array(4294967296); //232 = 4294967296
     var namelistC = new Array(-100) //負數
     
     console.log(namelistA.length); //RangeError: Invalid array length
    @@ -32,7 +32,7 @@ console.log(namelistB.length);
     
     

    你可以透過改變 length 屬性來改變陣列的長度。當你透過 length 屬性來增加陣列的長度時,陣列中實際的元素也會隨之增加。舉例來說,當你將 array.length 由 2 增加為3,則改動後該陣列即擁有3個元素,該新增的元素則會是一個不可迭代(non-iterable)的空槽(empty slot)。

    -
    const arr = [1, 2];
    +
    const arr = [1, 2];
     console.log(arr);
     // [ 1, 2 ]
     
    @@ -62,7 +62,7 @@ arr.forEach(element => console.log(element)); // 空元素無法被迭代
     
     

    以下範例中, 陣列 numbers 透過 length 屬性進行迭代操作,並將其內容值加倍。

    -
    var numbers = [1, 2, 3, 4, 5];
    +
    var numbers = [1, 2, 3, 4, 5];
     var length = numbers.length;
     for (var i = 0; i < length; i++) {
       numbers[i] *= 2;
    @@ -74,7 +74,7 @@ for (var i = 0; i < length; i++) {
     
     

    以下範例中, 陣列 numbers  的長度若大於 3,則將其長度縮減至 3。

    -
    var numbers = [1, 2, 3, 4, 5];
    +
    var numbers = [1, 2, 3, 4, 5];
     
     if (numbers.length > 3) {
       numbers.length = 3;
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/slice/index.html b/files/zh-tw/web/javascript/reference/global_objects/array/slice/index.html
    index e9cb1fb02c..7079acee9d 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/array/slice/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/array/slice/index.html
    @@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/slice
     
     

    語法

    -
    arr.slice([begin[, end]])
    +
    arr.slice([begin[, end]])
     

    參數

    @@ -56,7 +56,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/slice

    Return a portion of an existing array

    -
    var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
    +
    var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
     var citrus = fruits.slice(1, 3);
     
     // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
    @@ -67,7 +67,7 @@ var citrus = fruits.slice(1, 3);
     
     

    In the following example, slice creates a new array, newCar, from myCar. Both include a reference to the object myHonda. When the color of myHonda is changed to purple, both arrays reflect the change.

    -
    // Using slice, create newCar from myCar.
    +
    // Using slice, create newCar from myCar.
     var myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
     var myCar = [myHonda, 2, 'cherry condition', 'purchased 1997'];
     var newCar = myCar.slice(0, 2);
    @@ -90,7 +90,7 @@ console.log('newCar[0].color = ' + newCar[0].color);
     
     

    This script writes:

    -
    myCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2,
    +
    myCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2,
              'cherry condition', 'purchased 1997']
     newCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2]
     myCar[0].color = red
    @@ -104,7 +104,7 @@ newCar[0].color = purple
     
     

    slice method can also be called to convert Array-like objects / collections to a new Array. You just bind the method to the object. The {{jsxref("Functions/arguments", "arguments")}} inside a function is an example of an 'array-like object'.

    -
    function list() {
    +
    function list() {
       return Array.prototype.slice.call(arguments);
     }
     
    @@ -113,7 +113,7 @@ var list1 = list(1, 2, 3); // [1, 2, 3]
     
     

    Binding can be done with the .call function of {{jsxref("Function.prototype")}} and it can also be reduced using [].slice.call(arguments) instead of Array.prototype.slice.call. Anyway, it can be simplified using {{jsxref("Function.prototype.bind", "bind")}}.

    -
    var unboundSlice = Array.prototype.slice;
    +
    var unboundSlice = Array.prototype.slice;
     var slice = Function.prototype.call.bind(unboundSlice);
     
     function list() {
    @@ -127,7 +127,7 @@ var list1 = list(1, 2, 3); // [1, 2, 3]
     
     

    Although host objects (such as DOM objects) are not required by spec to follow the Mozilla behavior when converted by Array.prototype.slice and IE < 9 does not do so, versions of IE starting with version 9 do allow this. “Shimming” it can allow reliable cross-browser behavior. As long as other modern browsers continue to support this ability, as currently do IE, Mozilla, Chrome, Safari, and Opera, developers reading (DOM-supporting) slice code relying on this shim will not be misled by the semantics; they can safely rely on the semantics to provide the now apparently de facto standard behavior. (The shim also fixes IE to work with the second argument of slice() being an explicit {{jsxref("null")}}/{{jsxref("undefined")}} value as earlier versions of IE also did not allow but all modern browsers, including IE >= 9, now do.)

    -
    /**
    +
    /**
      * Shim for "fixing" IE's lack of support (IE < 9) for applying slice
      * on host objects like NamedNodeMap, NodeList, and HTMLCollection
      * (technically, since host objects have been implementation-dependent,
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/date/getday/index.html b/files/zh-tw/web/javascript/reference/global_objects/date/getday/index.html
    index fb55827283..18d250f6fd 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/date/getday/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/date/getday/index.html
    @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDay
     
     

    語法

    -
    dateObj.getDay()
    +
    dateObj.getDay()

    返回值

    @@ -25,7 +25,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDay

    下面第二行表示根據日期對象'Xmas95'的值,把1賦值給'weekday'。則1995年12月25日是星期一。

    -
    var Xmas95 = new Date('December 25, 1995 23:15:30');
    +
    var Xmas95 = new Date('December 25, 1995 23:15:30');
     var weekday = Xmas95.getDay();
     
     console.log(weekday); // 1
    @@ -34,7 +34,7 @@ console.log(weekday); // 1
     

    Note: 如果需要,可以使用{{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}加上options參數來獲取星期幾全名。使使用此方法能輕鬆做出各種國際語言:

    -
    var options = { weekday: 'long'};
    +
    var options = { weekday: 'long'};
     console.log(new Intl.DateTimeFormat('en-US', options).format(Xmas95));
     // Monday
     console.log(new Intl.DateTimeFormat('de-DE', options).format(Xmas95));
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/math/pow/index.html b/files/zh-tw/web/javascript/reference/global_objects/math/pow/index.html
    index 2bf7ffdc68..7119ba5862 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/math/pow/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/math/pow/index.html
    @@ -11,7 +11,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/pow
     
     

    語法

    -
    Math.pow(base, exponent)
    +
    Math.pow(base, exponent)

    參數

    @@ -36,7 +36,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/pow

    使用 Math.pow()

    -
    // simple
    +
    // simple
     Math.pow(7, 2);    // 49
     Math.pow(7, 3);    // 343
     Math.pow(2, 10);   // 1024
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/math/random/index.html b/files/zh-tw/web/javascript/reference/global_objects/math/random/index.html
    index 1613c18777..896d4ab948 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/math/random/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/math/random/index.html
    @@ -18,7 +18,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random
     
     

    語法

    -
    Math.random()
    +
    Math.random()

    回傳值 Return value

    @@ -30,7 +30,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random

    Getting a random number between 0 (inclusive) and 1 (exclusive)

    -
    function getRandom() {
    +
    function getRandom() {
       return Math.random();
     }
     
    @@ -39,7 +39,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random

    This example returns a random number between the specified values. The returned value is no lower than (and may possibly equal) min, and is less than (and not equal) max.

    -
    function getRandomArbitrary(min, max) {
    +
    function getRandomArbitrary(min, max) {
       return Math.random() * (max - min) + min;
     }
     
    @@ -48,7 +48,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random

    This example returns a random integer between the specified values. The value is no lower than min (or the next integer greater than min if min isn't an integer), and is less than (but not equal to) max.

    -
    function getRandomInt(min, max) {
    +
    function getRandomInt(min, max) {
       min = Math.ceil(min);
       max = Math.floor(max);
       return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
    @@ -63,7 +63,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random
     
     

    While the getRandomInt() function above is inclusive at the minimum, it's exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? The getRandomIntInclusive() function below accomplishes that.

    -
    function getRandomIntInclusive(min, max) {
    +
    function getRandomIntInclusive(min, max) {
       min = Math.ceil(min);
       max = Math.floor(max);
       return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html
    index addec4288b..99a2ef5dc6 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html
    @@ -18,7 +18,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Number/toFixed
     
     

    語法

    -
    numObj.toFixed([digits])
    +
    numObj.toFixed([digits])

    參數

    @@ -48,7 +48,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Number/toFixed

    Using toFixed

    -
    var numObj = 12345.6789;
    +
    var numObj = 12345.6789;
     
     numObj.toFixed();       // Returns '12346': note rounding, no fractional part
     numObj.toFixed(1);      // Returns '12345.7': note rounding
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/object/assign/index.html b/files/zh-tw/web/javascript/reference/global_objects/object/assign/index.html
    index f4dfca5af7..65330a6196 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/object/assign/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/object/assign/index.html
    @@ -9,7 +9,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Object/assign
     
     

    語法

    -
    Object.assign(target, ...sources)
    +
    Object.assign(target, ...sources)

    參數

    @@ -40,7 +40,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Object/assign

    複製物件

    -
    var obj = { a: 1 };
    +
    var obj = { a: 1 };
     var copy = Object.assign({}, obj);
     console.log(copy); // { a: 1 }
     
    @@ -49,7 +49,7 @@ console.log(copy); // { a: 1 }

    深層複製(deep clone)需要使用其他的替代方案,因為 Object.assign() 僅複製屬性值。若來源物件的值參照到一個子物件,它只會複製該子物件的參照。

    -
    function test() {
    +
    function test() {
       let a = { b: {c:4} , d: { e: {f:1}} }
       let g = Object.assign({},a) // 淺層
       let h = JSON.parse(JSON.stringify(a)); // 深層
    @@ -70,7 +70,7 @@ test();
     
     

    合併物件

    -
    var o1 = { a: 1 };
    +
    var o1 = { a: 1 };
     var o2 = { b: 2 };
     var o3 = { c: 3 };
     
    @@ -80,7 +80,7 @@ console.log(o1);  // { a: 1, b: 2, c: 3 }, 目標物件本身也被改變。有相同屬性時合併物件
     
    -
    var o1 = { a: 1, b: 1, c: 1 };
    +
    var o1 = { a: 1, b: 1, c: 1 };
     var o2 = { b: 2, c: 2 };
     var o3 = { c: 3 };
     
    @@ -91,7 +91,7 @@ console.log(obj); // { a: 1, b: 2, c: 3 },屬性c為o3.c的值,最後一個
     
     

    複製 Symbol 型別的屬性

    -
    var o1 = { a: 1 };
    +
    var o1 = { a: 1 };
     var o2 = { [Symbol('foo')]: 2 };
     
     var obj = Object.assign({}, o1, o2);
    @@ -101,7 +101,7 @@ Object.getOwnPropertySymbols(obj); // [Symbol(foo)]非不在
     
     

    在屬性鏈中的不可列舉屬性不會被複製

    -
    var obj = Object.create({ foo: 1 }, { // foo 是 obj 的屬性鏈。
    +
    var obj = Object.create({ foo: 1 }, { // foo 是 obj 的屬性鏈。
       bar: {
         value: 2  // bar 是不可列舉的屬性,因為enumerable預設為false。
       },
    @@ -117,7 +117,7 @@ console.log(copy); // { baz: 3 }
     
     

    原始型別會被包成物件

    -
    var v1 = 'abc';
    +
    var v1 = 'abc';
     var v2 = true;
     var v3 = 10;
     var v4 = Symbol('foo');
    @@ -130,7 +130,7 @@ console.log(obj); // { "0": "a", "1": "b", "2": "c" }
     
     

    任何異常將會中斷正進行的複製程序

    -
    var target = Object.defineProperty({}, 'foo', {
    +
    var target = Object.defineProperty({}, 'foo', {
       value: 1,
       writable: false
     }); // target.foo 是 read-only (唯讀)屬性
    @@ -148,7 +148,7 @@ console.log(target.baz);  // undefined, 第三個來源物件也不會被複製
     
     

    複製的存取程序

    -
    var obj = {
    +
    var obj = {
       foo: 1,
       get bar() {
         return 2;
    @@ -187,7 +187,7 @@ console.log(copy);
     
     

    {{Glossary("Polyfill","polyfill")}} 不支援Symbol屬性,因為ES5沒有Symbol型別。

    -
    if (typeof Object.assign != 'function') {
    +
    if (typeof Object.assign != 'function') {
       Object.assign = function (target, varArgs) { // .length of function is 2
         'use strict';
         if (target == null) { // TypeError if undefined or null
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/object/index.html b/files/zh-tw/web/javascript/reference/global_objects/object/index.html
    index 3885c44a15..cf55c9b004 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/object/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/object/index.html
    @@ -15,7 +15,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Object
     
     

    語法

    -
    // Object initialiser or literal
    +
    // Object initialiser or literal
     { [ nameValuePair1[, nameValuePair2[, ...nameValuePairN] ] ] }
     
     // Called as a constructor
    @@ -110,24 +110,24 @@ new Object([value])

    下面例子儲存一個空物件至變數o

    -
    var o = new Object();
    +
    var o = new Object();
     
    -
    var o = new Object(undefined);
    +
    var o = new Object(undefined);
     
    -
    var o = new Object(null);
    +
    var o = new Object(null);
     

    Using Object to create Boolean objects

    下面例子儲存 {{jsxref("Boolean")}} 物件在 o:

    -
    // equivalent to o = new Boolean(true);
    +
    // equivalent to o = new Boolean(true);
     var o = new Object(true);
     
    -
    // equivalent to o = new Boolean(false);
    +
    // equivalent to o = new Boolean(false);
     var o = new Object(Boolean());
     
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html b/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html index 54a71be888..f41b0a6caa 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html +++ b/files/zh-tw/web/javascript/reference/global_objects/proxy/index.html @@ -27,7 +27,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Proxy

    語法

    -
    var p = new Proxy(target, handler);
    +
    var p = new Proxy(target, handler);
     

    參數

    @@ -58,7 +58,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Proxy

    In this simple example the number 37 gets returned as the default value when the property name is not in the object. It is using the get handler.

    -
    var handler = {
    +
    var handler = {
         get: function(target, name) {
             return name in target ?
                 target[name] :
    @@ -78,7 +78,7 @@ console.log('c' in p, p.c); // false, 37
     
     

    In this example, we are using a native JavaScript object to which our proxy will forward all operations that are applied to it.

    -
    var target = {};
    +
    var target = {};
     var p = new Proxy(target, {});
     
     p.a = 37; // operation forwarded to the target
    @@ -90,7 +90,7 @@ console.log(target.a); // 37. The operation has been properly forwarded
     
     

    With a Proxy, you can easily validate the passed value for an object. This example uses the set handler.

    -
    let validator = {
    +
    let validator = {
       set: function(obj, prop, value) {
         if (prop === 'age') {
           if (!Number.isInteger(value)) {
    @@ -120,7 +120,7 @@ person.age = 300; // Throws an exception

    A function proxy could easily extend a constructor with a new constructor. This example uses the construct and apply handlers.

    -
    function extend(sup, base) {
    +
    function extend(sup, base) {
       var descriptor = Object.getOwnPropertyDescriptor(
         base.prototype, 'constructor'
       );
    @@ -161,7 +161,7 @@ console.log(Peter.age);  // 13

    Sometimes you want to toggle the attribute or class name of two different elements. Here's how using the set handler.

    -
    let view = new Proxy({
    +
    let view = new Proxy({
       selected: null
     },
     {
    @@ -196,7 +196,7 @@ console.log(i2.getAttribute('aria-selected')); // 'true'

    The products proxy object evaluates the passed value and convert it to an array if needed. The object also supports an extra property called latestBrowser both as a getter and a setter.

    -
    let products = new Proxy({
    +
    let products = new Proxy({
       browsers: ['Internet Explorer', 'Netscape']
     },
     {
    @@ -241,7 +241,7 @@ console.log(products.latestBrowser); // 'Chrome'

    This proxy extends an array with some utility features. As you see, you can flexibly "define" properties without using Object.defineProperties. This example can be adapted to find a table row by its cell. In that case, the target will be table.rows.

    -
    let products = new Proxy([
    +
    let products = new Proxy([
       { name: 'Firefox', type: 'browser' },
       { name: 'SeaMonkey', type: 'browser' },
       { name: 'Thunderbird', type: 'mailer' }
    @@ -302,7 +302,7 @@ console.log(products.number); // 3
     
     

    Now in order to create a complete sample traps list, for didactic purposes, we will try to proxify a non native object that is particularly suited to this type of operation: the docCookies global object created by the "little framework" published on the document.cookie page.

    -
    /*
    +
    /*
       var docCookies = ... get the "docCookies" object here:
       https://developer.mozilla.org/en-US/docs/DOM/document.cookie#A_little_framework.3A_a_complete_cookies_reader.2Fwriter_with_full_unicode_support
     */
    diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/index.html
    index 2b3f80fdd1..7875873120 100644
    --- a/files/zh-tw/web/javascript/reference/global_objects/set/index.html
    +++ b/files/zh-tw/web/javascript/reference/global_objects/set/index.html
    @@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Set
     
     

    語法

    -
    new Set([iterable]);
    +
    new Set([iterable]);

    參數

    @@ -67,7 +67,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Set

    使用 Set 物件

    -
    var mySet = new Set();
    +
    var mySet = new Set();
     
     mySet.add(1); // Set [ 1 ]
     mySet.add(5); // Set [ 1, 5 ]
    @@ -95,7 +95,7 @@ console.log(mySet);// Set [ 1, "some text", Object {a: 1, b: 2}, Object {a: 1, b
     
     

    迭代 Sets

    -
    // iterate over items in set
    +
    // iterate over items in set
     // logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
     for (let item of mySet) console.log(item);
     
    @@ -139,7 +139,7 @@ mySet.forEach(function(value) {
     
     

    實作基本的 set 操作

    -
    Set.prototype.isSuperset = function(subset) {
    +
    Set.prototype.isSuperset = function(subset) {
         for (var elem of subset) {
             if (!this.has(elem)) {
                 return false;
    @@ -188,7 +188,7 @@ setA.difference(setC); // => Set [1, 2]
     
     

    與 Array 物件關聯

    -
    var myArray = ['value1', 'value2', 'value3'];
    +
    var myArray = ['value1', 'value2', 'value3'];
     
     // Use the regular Set constructor to transform an Array into a Set
     var mySet = new Set(myArray);
    @@ -200,7 +200,7 @@ console.log([...mySet]); // Will show you exactly the same Array as myArray
    與 Strings 關聯 -
    var text = 'India';
    +
    var text = 'India';
     
     var mySet = new Set(text);  // Set ['I', 'n', 'd', 'i', 'a']
     mySet.size;  // 5
    diff --git a/files/zh-tw/web/javascript/reference/operators/conditional_operator/index.html b/files/zh-tw/web/javascript/reference/operators/conditional_operator/index.html
    index cd0ccfa160..cd7f851374 100644
    --- a/files/zh-tw/web/javascript/reference/operators/conditional_operator/index.html
    +++ b/files/zh-tw/web/javascript/reference/operators/conditional_operator/index.html
    @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Operators/Conditional_Operator
     
     

    語法

    -
    condition ? exprIfTrue : exprIfFalse
    +
    condition ? exprIfTrue : exprIfFalse

    參數

    @@ -32,14 +32,14 @@ translation_of: Web/JavaScript/Reference/Operators/Conditional_Operator

    一個簡單的範例:

    -
    var age = 26;
    +
    var age = 26;
     var beverage = (age >= 21) ? "Beer" : "Juice";
     console.log(beverage); // "Beer"
     

    一個常用來處理 null 的用法 : 

    -
    function greeting(person) {
    +
    function greeting(person) {
         var name = person ? person.name : "stranger";
         return "Howdy, " + name;
     }
    @@ -52,7 +52,7 @@ console.log(greeting(null));             // "Howdy, stranger"
     
     

    條件 (三元) 運算子是右相依性的 (right-associative), 代表他可以以下面的方式鏈結 , 類似於 if … else if … else if … else 的鏈結方法 :

    -
    function example(…) {
    +
    function example(…) {
         return condition1 ? value1
              : condition2 ? value2
              : condition3 ? value3
    diff --git a/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html b/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html
    index fe6ea9a383..42793870c0 100644
    --- a/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html
    +++ b/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html
    @@ -15,16 +15,16 @@ translation_of: Web/JavaScript/Reference/Operators/Spread_syntax
     
     

    用在呼叫函式時:

    -
    myFunction(...iterableObj);
    +
    myFunction(...iterableObj);
     

    用在陣列或字串時:

    -
    [...iterableObj, '4', 'five', 6];
    +
    [...iterableObj, '4', 'five', 6];

    用在物件時(new in ECMAScript 2018):

    -
    let objClone = { ...obj };
    +
    let objClone = { ...obj };

    Rest syntax (parameters)

    @@ -38,19 +38,19 @@ translation_of: Web/JavaScript/Reference/Operators/Spread_syntax

    It is common to use {{jsxref("Function.prototype.apply()")}} in cases where you want to use the elements of an array as arguments to a function.

    -
    function myFunction(x, y, z) { }
    +
    function myFunction(x, y, z) { }
     const args = [0, 1, 2];
     myFunction.apply(null, args);

    With spread syntax the above can be written as:

    -
    function myFunction(x, y, z) { }
    +
    function myFunction(x, y, z) { }
     const args = [0, 1, 2];
     myFunction(...args);

    Any argument in the argument list can use spread syntax, and the spread syntax can be used multiple times.

    -
    function myFunction(v, w, x, y, z) { }
    +
    function myFunction(v, w, x, y, z) { }
     const args = [0, 1];
     myFunction(-1, ...args, 2, ...[3]);
    @@ -58,13 +58,13 @@ myFunction(-1, ...args, 2, ...[3]);

    When calling a constructor with {{jsxref("Operators/new", "new")}} it's not possible to directly use an array and apply() (apply() does a [[Call]] and not a [[Construct]]). However, an array can be easily used with new thanks to spread syntax:

    -
    const dateFields = [1970, 0, 1];  // 1 Jan 1970
    +
    const dateFields = [1970, 0, 1];  // 1 Jan 1970
     const d = new Date(...dateFields);
     

    To use new with an array of parameters without spread syntax, you would have to do it indirectly through partial application:

    -
    function applyAndNew(constructor, args) {
    +
    function applyAndNew(constructor, args) {
        function partial () {
           return constructor.apply(this, args);
        };
    @@ -96,7 +96,7 @@ console.log(new myConstructorWithArguments);
     
     

    Without spread syntax, to create a new array using an existing array as one part of it, the array literal syntax is no longer sufficient and imperative code must be used instead using a combination of {{jsxref("Array.prototype.push", "push()")}}, {{jsxref("Array.prototype.splice", "splice()")}}, {{jsxref("Array.prototype.concat", "concat()")}}, etc. With spread syntax this becomes much more succinct:

    -
    const parts = ['shoulders', 'knees'];
    +
    const parts = ['shoulders', 'knees'];
     const lyrics = ['head', ...parts, 'and', 'toes'];
     //  ["head", "shoulders", "knees", "and", "toes"]
     
    @@ -105,7 +105,7 @@ const lyrics = ['head', ...parts, 'and', 'toes'];

    Copy an array

    -
    const arr = [1, 2, 3];
    +
    const arr = [1, 2, 3];
     const arr2 = [...arr]; // like arr.slice()
     
     arr2.push(4);
    @@ -116,7 +116,7 @@ arr2.push(4);
     

    Note: Spread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays, as the following example shows. (The same is true with {{jsxref("Object.assign()")}} and spread syntax.)

    -
    const a = [[1], [2], [3]];
    +
    const a = [[1], [2], [3]];
     const b = [...a];
     
     b.shift().shift();
    @@ -132,7 +132,7 @@ a
     
     

    {{jsxref("Array.prototype.concat()")}} is often used to concatenate an array to the end of an existing array. Without spread syntax, this is done as:

    -
    const arr1 = [0, 1, 2];
    +
    const arr1 = [0, 1, 2];
     const arr2 = [3, 4, 5];
     
     //  Append all items from arr2 onto arr1
    @@ -140,7 +140,7 @@ arr1 = arr1.concat(arr2);

    With spread syntax this becomes:

    -
    let arr1 = [0, 1, 2];
    +
    let arr1 = [0, 1, 2];
     let arr2 = [3, 4, 5];
     
     arr1 = [...arr1, ...arr2];
    @@ -150,7 +150,7 @@ arr1 = [...arr1, ...arr2];
     
     

    {{jsxref("Array.prototype.unshift()")}} is often used to insert an array of values at the start of an existing array. Without spread syntax, this is done as:

    -
    const arr1 = [0, 1, 2];
    +
    const arr1 = [0, 1, 2];
     const arr2 = [3, 4, 5];
     
     //  Prepend all items from arr2 onto arr1
    @@ -160,7 +160,7 @@ Array.prototype.unshift.apply(arr1, arr2)
     
     

    With spread syntax, this becomes:

    -
    let arr1 = [0, 1, 2];
    +
    let arr1 = [0, 1, 2];
     let arr2 = [3, 4, 5];
     
     arr1 = [...arr2, ...arr1];
    @@ -177,7 +177,7 @@ arr1 = [...arr2, ...arr1];
     
     

    Shallow-cloning (excluding prototype) or merging of objects is now possible using a shorter syntax than {{jsxref("Object.assign()")}}.

    -
    const obj1 = { foo: 'bar', x: 42 };
    +
    const obj1 = { foo: 'bar', x: 42 };
     const obj2 = { foo: 'baz', y: 13 };
     
     const clonedObj = { ...obj1 };
    @@ -190,7 +190,7 @@ const mergedObj = { ...obj1, ...obj2 };
     
     

    Note that you cannot replace or mimic the {{jsxref("Object.assign()")}} function:

    -
    let obj1 = { foo: 'bar', x: 42 };
    +
    let obj1 = { foo: 'bar', x: 42 };
     let obj2 = { foo: 'baz', y: 13 };
     const merge = ( ...objects ) => ( { ...objects } );
     
    @@ -208,7 +208,7 @@ let mergedObj2 = merge ({}, obj1, obj2);
     
     

    Spread syntax (other than in the case of spread properties) can be applied only to iterable objects:

    -
    const obj = {'key1': 'value1'};
    +
    const obj = {'key1': 'value1'};
     const array = [...obj]; // TypeError: obj is not iterable
     
    diff --git a/files/zh-tw/web/javascript/reference/statements/if...else/index.html b/files/zh-tw/web/javascript/reference/statements/if...else/index.html index a9317aa8a6..9224af1894 100644 --- a/files/zh-tw/web/javascript/reference/statements/if...else/index.html +++ b/files/zh-tw/web/javascript/reference/statements/if...else/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Statements/if...else

    語法

    -
    if (條件式)
    +
    if (條件式)
        statement1
     [else
        statement2]
    @@ -39,7 +39,7 @@ translation_of: Web/JavaScript/Reference/Statements/if...else
     
     

    多重的 if...else 陳述式可以使用 else if 子句來建立一個巢狀結構的句子。要記住,在JavaScript中沒有 elseif (一個單字) 的語法可以用。

    -
    if (condition1)
    +
    if (condition1)
        statement1
     else if (condition2)
        statement2
    @@ -52,7 +52,7 @@ else
     
     

    將巢狀結構適當的排版後,我們能更了解其背後運作的邏輯:

    -
    if (condition1)
    +
    if (condition1)
        statement1
     else
        if (condition2)
    @@ -64,7 +64,7 @@ else
     
     

    如果在一個條件式中有多個陳述要執行,可以使用區塊陳述式({ ... }) 把所有陳述包在一起。 通常來說,無論如何都使用區塊陳述式是個很好的習慣,尤其是當你使用巢狀結構的 if 陳述式時,這會讓人更容易理解你的程式碼。

    -
    if (condition) {
    +
    if (condition) {
        statements1
     } else {
        statements2
    @@ -73,7 +73,7 @@ else
     
     

    不要被Boolean物件中,布林值的 truefalse 給混淆了。任何值只要不是 falseundefinednull0NaN,或者空字串 (""),並且任何物件,包括其值是 false的布林物件 ,仍然會被條件陳述式視為條件成立。舉例而言:

    -
    var b = new Boolean(false);
    +
    var b = new Boolean(false);
     if (b) // this condition is truthy
     
    @@ -81,7 +81,7 @@ if (b) // this condition is truthy

    使用 if...else

    -
    if (cipher_char === from_char) {
    +
    if (cipher_char === from_char) {
        result = result + to_char;
        x++;
     } else {
    @@ -93,7 +93,7 @@ if (b) // this condition is truthy
     
     

    要記得JavaScript沒有 elseif 可以使用。不過,你可以使用 elseif中間夾著空白的語法:

    -
    if (x > 5) {
    +
    if (x > 5) {
      /* do the right thing */
     } else if (x > 50) {
      /* do the right thing */
    @@ -105,14 +105,14 @@ if (b) // this condition is truthy
     
     

    建議不要在條件表達式中直接對物件賦值,因為這會使人在瀏覽程式碼時很容易將賦值( assignment )與相等( equality )混淆。舉例而言,不要使用以下寫法:

    -
    if (x = y) {
    +
    if (x = y) {
        /* do the right thing */
     }
     

    如果你必須在條件表達式中使用賦值,最好ˇ的作法是以額外的括號包住賦值語句,如下所示:

    -
    if ((x = y)) {
    +
    if ((x = y)) {
        /* do the right thing */
     }
     
    diff --git a/files/zh-tw/web/javascript/reference/statements/let/index.html b/files/zh-tw/web/javascript/reference/statements/let/index.html index 0cdc8806be..f3170bb603 100644 --- a/files/zh-tw/web/javascript/reference/statements/let/index.html +++ b/files/zh-tw/web/javascript/reference/statements/let/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Statements/let

    語法

    -
    let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];
    +
    let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];

    參數

    @@ -32,7 +32,7 @@ translation_of: Web/JavaScript/Reference/Statements/let

    宣告 let 的作用範圍是它們被定義的區塊,以及該區塊包含的子區塊。這樣看起來功能跟 var 很相似。主要不同的地方在於 var 作用範圍是「整個」function:

    -
    function varTest() {
    +
    function varTest() {
       var x = 1;
       {
         var x = 2;  // 這裡的 x 與 function 區塊內部的 x 是一樣的,因此會影響 function 區塊內所有的 x
    @@ -52,7 +52,7 @@ function letTest() {
     
     

    在上列例子裡的最前行 letvar 不同,let 並不會在全域物件中建立變數。舉例來說:

    -
    var x = 'global';
    +
    var x = 'global';
     let y = 'global';
     console.log(this.x); // "global"
     console.log(this.y); // undefined
    @@ -62,7 +62,7 @@ console.log(this.y); // undefined
     
     

    In dealing with constructors it is possible to use the let bindings to share one or more private members without using closures:

    -
    var Thing;
    +
    var Thing;
     
     {
       let privateScope = new WeakMap();
    @@ -104,21 +104,21 @@ thing.showPrivate();
     
     

    Redeclaring the same variable within the same function or block scope raises a {{jsxref("SyntaxError")}}.

    -
    if (x) {
    +
    if (x) {
       let foo;
       let foo; // SyntaxError thrown.
     }

    In ECMAScript 2015, let bindings are not subject to Variable Hoisting, which means that let declarations do not move to the top of the current execution context. Referencing the variable in the block before the initialization results in a ReferenceError (contrary to a variable declared with var, which will just have the undefined value). The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.

    -
    function do_something() {
    +
    function do_something() {
       console.log(foo); // ReferenceError
       let foo = 2;
     }

    你可能會在 switch 中遇到錯誤,因為所有的 case 都屬於同樣的區塊中。

    -
    switch (x) {
    +
    switch (x) {
       case 0:
         let foo;
         break;
    @@ -132,7 +132,7 @@ thing.showPrivate();
     
     

    You can use the let keyword to bind variables locally in the scope of for loops. This is different from the var keyword in the head of a for loop, which makes the variables visible in the whole function containing the loop.

    -
    var i=0;
    +
    var i=0;
     for ( let i=i ; i < 10 ; i++ ) {
       console.log(i);
     }
    @@ -140,7 +140,7 @@ for ( let i=i ; i < 10 ; i++ ) {
     
     

    However, it's important to point out that a block nested inside a case clause will create a new block scoped lexical environment, which will not produce the redeclaration errors shown above.

    -
    let x = 1;
    +
    let x = 1;
     
     switch(x) {
       case 0: {
    @@ -157,7 +157,7 @@ switch(x) {
     
     

    Unlike with simply undeclared variables and variables that hold a value of undefined, using the typeof operator to check for the type of a variable in that variable's TDZ will throw a ReferenceError:

    -
    // prints out 'undefined'
    +
    // prints out 'undefined'
     console.log(typeof undeclaredVariable);
     // results in a 'ReferenceError'
     console.log(typeof i);
    @@ -168,7 +168,7 @@ let i = 10;

    Due to lexical scoping, the identifier "foo" inside the expression (foo + 55) evaluates to the if block's foo, and not the overlying variable foo with the value of 33.
    In that very line, the if block's "foo" has already been created in the lexical environment, but has not yet reached (and terminated) its initialization (which is part of the statement itself): it's still in the temporal dead zone.

    -
    function test(){
    +
    function test(){
        var foo = 33;
        {
           let foo = (foo + 55); // ReferenceError
    @@ -178,7 +178,7 @@ test();

    This phenomenon may confuse you in a situation like the following. The instruction let n of n.a is already inside the private scope of the for loop's block, hence the identifier "n.a" is resolved to the property 'a' of the 'n' object located in the first part of the instruction itself ("let n"), which is still in the temporal dead zone since its declaration statement has not been reached and terminated.

    -
    function go(n) {
    +
    function go(n) {
       // n here is defined!
       console.log(n); // Object {a: [1,2,3]}
     
    @@ -194,7 +194,7 @@ go({a: [1, 2, 3]});
     
     

    When used inside a block, let limits the variable's scope to that block. Note the difference between var whose scope is inside the function where it is declared.

    -
    var a = 1;
    +
    var a = 1;
     var b = 2;
     
     if (a === 1) {
    -- 
    cgit v1.2.3-54-g00ecf