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 --- .../functions/default_parameters/index.html | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'files/zh-tw/web/javascript/reference/functions/default_parameters') 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;
 }
 
-- 
cgit v1.2.3-54-g00ecf