variable is in main function then why is it out of scope

时间:2018-03-23 02:11:15

标签: javascript scope

I have created variable upperCaseAfter in the main function than why I get the warning out of scope when I used it later by assigning to arr[i]?

function myReplace(str, before, after) {
  arr =str.split(" ");
  //to adjust upper or lower cases before we even replace the words and store it in new variable if after value has to be changed 

  for (var i=0; i<arr.length; i++) {   
    if(before[0]===before[0].toUpperCase()){
     var upperCaseAfter=after[0].toUpperCase() + after.substring(1);   
    }
    if (arr[i]===before &&  before[0]===before[0].toUpperCase()){          
      arr[i]=upperCaseAfter;      
     } else 
     if(arr[i]==before && before[0]===before[0].toLowerCase()) {
          arr[i]=after;
     }      

   }
   str=arr.join(" ");       
 return str;
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

3 个答案:

答案 0 :(得分:0)

您在#include <iostream> #include <string> #include <sstream> using namespace std; int main(int argc, char* argv[]) { unsigned int m; unsigned int n; argc = 3; const char* args[3]; args[1] = "-1"; args[2] = "-2"; istringstream iss; if (argc != 3) // Passes because argument count is 3 { cerr << "Usage: " << argv[0] << " <integer m> <integer n>" << endl; return 1; } iss.str(args[1]); // iss now holds "-1" // istringstream operator >> will set either eofbit, failbit or badbit // on failure. The iss object is convertible to bool so you can check // in an if statement. if (!(iss >> m)) // Extracts "-1" into unsigned int m. // m holds 4294967295 because it's unsigned. // if statement doesn't fail because istringstream can // can extract into signed ints also { cerr << "Error: The first argument is not a valid nonnegative integer." << endl; return 1; } iss.clear(); iss.str(args[2]); // iss object now holds "-2" if (!(iss >> n)) // Same here, extraction into unsigned in doesn't fail { cerr << "Error: The second argument is not a valid nonnegative integer." << endl; return 1; } if (n > m) // This checks if n > m, but still nowhere in the code have you // checked if the argv[] values are negative or positive values. { cerr << "Error: The second argument is not a valid nonnegative integer." << endl; return 1; } cout << m << " x " << n << " = " << endl; return 0; } 函数内重新声明了它:

myReplace

当您执行以下操作时,JS编译器认为您要在if(before[0]===before[0].toUpperCase()){ var upperCaseAfter=after[0].toUpperCase() + after.substring(1); } 内引用此变量:

myReplace

然后它超出了范围,因为你在arr[i]=upperCaseAfter; 语句中声明了它。

解决方案:在if功能内部将upperCaseAfter重命名为其他内容,或删除关键字myReplace以使用您在var中声明的upperCaseAfter(外部范围)。

答案 1 :(得分:0)

我想我找到了解决方案,我只需要在for循环之前声明变量upperCaseAfter,然后我就不会收到任何警告。

答案 2 :(得分:0)

我做了以下更改,将错误消除了!

function myReplace(str, before, after) {
      arr =str.split(" ");
      var upperCaseAfter;
  //to adjust upper or lower cases before we even replace the words and store it 
    in new variable if after value has to be changed 

  for (var i=0; i<arr.length; i++) {   
    if(before[0]===before[0].toUpperCase()){
     upperCaseAfter=after[0].toUpperCase() + after.substring(1);   
    }
    if (arr[i]===before &&  before[0]===before[0].toUpperCase()){          
      arr[i]=upperCaseAfter;      
     } else 
     if(arr[i]==before && before[0]===before[0].toLowerCase()) {
          arr[i]=after;
     }      

   }
   str=arr.join(" ");       
 return str;
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");