简体   繁体   English

有人可以向我解释这个函数的作用吗?

[英]Can someone please explain to me what this function does?

I started learning JavaScript a couple of weeks ago and i always find myself stuck when it comes to for loops.几周前我开始学习 JavaScript,但我总是发现自己在 for 循环方面陷入困境。

like in this example, i don't understand what the for loop does , can someone please give me some tips on how to understand this?就像在这个例子中一样,我不明白 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();
};

Comments in code will explain what is happening here代码中的注释将解释这里发生的事情

 //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'

The function adds all the values passed to it as a concatenated sting in all caps该函数将传递给它的所有值添加为所有大写字母的连接字符串

//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 (); }; 

This is an implementation of a template literal tag.这是模板文字标记的实现。 It would be invoked as in它将被调用为

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

which would evaluate to这将评估为

MY NAME IS BOB AND I AM 29 YEARS OLD

The template literal mechanism invokes the function named upper , passing as the parameter strings the parts of the template outside the ${} .模板文字机制调用名为upper的函数,将${}之外的模板部分作为参数strings传递。 In this case, it is ["My name is ", " and I am ", " years old"] .在这种情况下,它是["My name is ", " and I am ", " years old"] As values , it passes a list of the interpolated values coming in between the strings, which in this case is作为values ,它传递来自字符串之间的内插值列表,在这种情况下是

["Bob", 29]

The code loops through the values in strings .该代码循环遍历strings的值。 If there is a corresponding (following) interpolated value in values , then it sticks that in. Then it upper cases the entire result and returns it.如果values有相应的(以下)内插值,则将其插入。然后将整个结果大写并返回。

For more information on implementing your own template literal tags, see the documentation .有关实现您自己的模板文字标记的更多信息,请参阅文档

It replaces specific characters of a string at indexes of characters of you pass in as an array of values .它在您作为values数组传入的字符索引处替换字符串的特定字符。

However, one shortcoming - it does not supporting replacing the first character of a passed in string with a corresponding values array value.但是,一个缺点 - 它不支持用相应的values数组值替换传入string的第一个字符。

values can be either an array or string , but that does not solve the flaw of this function. values可以是arraystring ,但这并不能解决此函数的缺陷。

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

This is a function declaration, it stores an anonymous function in the variable "upper".这是一个函数声明,它在变量“upper”中存储了一个匿名函数。

 let result = '';  

This assigns an empty string to "result".这将一个空字符串分配给“result”。

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

for loop, iterates over the strings passed to the anonymous function and stores the results in "result". for 循环,遍历传递给匿名函数的字符串并将结果存储在“result”中。 If there are other parameters, these are stored in "result" as well.如果还有其他参数,它们也会存储在“result”中。

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

Here it calls console.log with result as argument, then returns the result value, after running the native toUpperCase method on result.在这里,它以结果为参数调用 console.log,然后在结果上运行本机 toUpperCase 方法后返回结果值。

You cannot have the function like that, it will not run, the arguments shouldn't be "...values".你不能拥有那样的函数,它不会运行,参数不应该是“...values”。 But that is what it does, takes a value and returns a string as uppercase lettered string.但这就是它所做的,接受一个值并以大写字母字符串形式返回一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM