2017-08-11 136 views
-4

幾個星期前我開始學習JavaScript,並且當涉及到循環時,我總是發現自己陷入了困境。有人可以向我解釋這個功能嗎?

就像在這個例子中,我不明白for循環是做什麼的,有人能給我一些關於如何理解這個的提示嗎?

let upper = function(strings, ...values) { 
    let result = ''; 
    for (var i = 0; i < strings.length; i++) { 
     result += strings[i]; 
     if (i < values.length) { 
      result += values[i]; 
     } 
    } 
    console.log(result); 
    return result.toUpperCase(); 
}; 
+0

是的,只有一個'for'循環。你有沒有不小心加入第二個? –

+1

在調試器中逐行執行代碼,觀察它執行的操作並檢查變量的值。 – 2017-08-11 09:30:18

+0

你如何調用'upper()'? – Durga

回答

2

評論中的代碼解釋這裏發生了什麼

//input is array with string and array with values. 
 
//strings, ...values - that is means that first argument will be strings, but all arguments since first will be pushed into array called values 
 
let upper = function(strings, ...values) { 
 
    console.log(strings); // ['a', 'b']; 
 
    console.log(values); // [1, 2]; 
 

 
    let result = ''; // result is an empty string 
 
    for (var i = 0; i < strings.length; i++) { // looping the array of string 
 
    result += strings[i]; // result = result + string from strings array at first iteration you will have result equal to 'a' 
 
    if (i < values.length) { //check do we have number i in values array 
 
     result += values[i]; // if yes than result = result + string from vaulues. at first iteration you will have result equal to 'a' + 1 that is equal to 'a1' 
 
    } // end of if 
 
    } // and so on 
 
    console.log(result); // 'a1b2' 
 
    return result.toUpperCase(); // returning result in uppercase 
 
}; 
 

 
var res = upper(['a', 'b'], 1, 2); 
 
console.log (res); // 'A1B2'

+1

第二個參數不應該是一個數組,實際上應該有任意數量的參數加入到'values'數組中,比如,如果u調用upper(['a','b'],'c','d','e','f')值數組將會是['c','d','e','f' ] –

+0

對不起,我錯過了...我會編輯一個答案 – qiAlex

+2

@Teemu不是擴展運算符,它被稱爲'Rest parameters'語法。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters –

-1

您在傳遞作爲values數組它取代了在字符的索引字符串的特定字符。

但是,有一個缺點 - 它不支持用相應的values數組值替換string中傳遞的第一個字符。

values可以是arraystring,但這並不能解決此功能的缺陷。

-1
let upper = function(strings, ...values) { 

這是一個函數聲明,它在變量「upper」中存儲一個匿名函數。

let result = ''; 

這會給「結果」分配一個空字符串。

for (var i = 0; i < strings.length; i++) { 
result += strings[i]; 
if (i < values.length) { 
    result += values[i]; 
} 
} 

for循環,遍歷傳遞給匿名函數的字符串並將結果存儲在「result」中。如果還有其他參數,這些參數也存儲在「結果」中。

console.log(result); 
return result.toUpperCase(); 
}; 

這調用的console.log與結果作爲參數,然後返回結果值,上運行結果本地toUpperCase方法之後。

你不能有這樣的功能,它不會運行,參數不應該是「...值」。但它就是這樣做的,它接受一個值並返回一個字符串作爲大寫字母字符串。

+0

*參數不應該是「...值」*。那是什麼意思? – 2017-08-11 09:46:01

+0

這只是意味着你不應該用「.... values」聲明函數參數。正確的方法是讓上=功能(字符串,...值) – ptts

+0

我很困惑。你說你不應該用「... values」聲明函數參數,但是你說正確的方法是「...... values」。你在說什麼,你應該做還是不應該做?這不僅僅是參數傳播語法嗎? – 2017-08-11 10:09:43

0

功能增加了傳遞給它的串聯刺痛了所有值全部大寫

//declares a function in block scope with parameter strings and values which is an array of all other values passed to the function 
let upper = function (strings , ... values) { 
/declares a empty string with block scope called results 
let result = '' ; 
//a for loop that uses i as a counter 
//i is incremented (increased by one) each iteration of the loop until i equals the variable that is passed to the function called strings' length 
for (var i = 0 ; i < strings . length ; i ++) { 
//adds strings[i] to result variable declared above 
result += strings [ i ]; 
//if I is lessthan values length than add values[i] to results 
//if I was greater than or equal to values.length than values[i] wouldn't exist and an error would be thrown or Null would be added to results 
if (i < values . length) { 
    result += values [ i ]; } } 
//outputs result to the console and returns a completely uppercase string from the result variable to what called the function 
console . log (result); return result . toUpperCase(); }; 
0

這是一個模板文字標籤的實現。這將被調用爲

upper`My name is ${name} and I am ${age} years old` 

這將評估爲

MY NAME IS BOB AND I AM 29 YEARS OLD 

模板字面機制調用名爲upper功能,傳遞作爲參數strings模板的部分外${}。在這種情況下,它是["My name is ", " and I am ", " years old"]。作爲values,它通過內插的值的列表來在串,在此情況下是

["Bob", 29] 

該代碼通過值strings環之間。如果在values中存在相應的(以下)插值,則將其插入。然後,將大寫整個結果並返回。

有關實現自己的模板文字標記的更多信息,請參閱the documentation

+0

這很簡單和有幫助。謝謝。 – JoeBirmen

相關問題