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 --- .../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 +++++++------- 18 files changed, 153 insertions(+), 153 deletions(-) (limited to 'files/zh-tw/web/javascript/reference') 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