简体   繁体   English

有没有办法在 JavaScript 中重置全局变量

[英]Is there any way to reset Global variable in JavaScript

I have a global variable with classname defined to it.我有一个定义了类名的全局变量。

var className = "My Class is.";

And I will use that className variable to add more我将使用该className变量添加更多

var a = function() {
  className = className + "a" ;
  console.log(className);
}    
console.log(className);
a();
console.log(className);

But after calling the function a() , variable className is still holding the data.但是在调用函数a() ,变量className仍然保存着数据。 I believe this is how JS behaves, but is there any way to reset className variable every time when it comes out of a function.我相信这就是 JS 的行为方式,但是有没有办法在每次从函数中出现时重置className变量。

Based on your comment :根据您的评论

I am defining a local variable named FunctionName with className:MethodName as format.我正在定义一个名为 FunctionName 的局部变量,其格式为 className:MethodName。 If I can define a variable with className, then I can use that instead of placing complete className in every method, just trying avoid retyping and spelling mistakes mostly.如果我可以用 className 定义一个变量,那么我可以使用它而不是在每个方法中放置完整的 className,只是尽量避免重新输入和拼写错误。

...I think you want to just use className without modifying it: ...我认为您只想使用className而不修改它:

var a = function() {
    var functionName = className + "a";
    // ...use functionName...
};

But if you really want to modify it for some reason, without using a local, then you can remember its old value and restore it:但是如果你出于某种原因真的想修改它,而不使用本地,那么你可以记住它的旧值并恢复它:

var a = function() {
    var savedClassName = className + "a";
    try {
        className = className + "a";
        // ...use className...
    } finally {
        className = savedClassName;
    }
};

No matter how you exit the code in the try block, the finally block will be executed.无论你如何退出try块中的代码, finally块都会被执行。

But there are very few use cases where that's the best approach.但是很少有用例是最好的方法。

If you really want to use a local variable with the same name, that is possible:如果你真的想使用同名的局部变量,那是可能的:

 var className = "My Class is."; // define global var let a = function() { let className = window.className; // define local var and copy global var value to local var className = className + "a"; // modify local var console.log(className); // print local var }; console.log(className); // print global var value a(); // does not change global var value console.log(className) // print unchanged global var value

But it would be much clearer to just have different names for your local and global variables:但是为局部变量和全局变量使用不同的名称会更清楚:

 var className = "My Class is."; let a = function() { let localClassName = className; localClassName = localClassName + "a"; console.log(localClassName) }; console.log(className); a(); console.log(className)

Two ways:两种方式:

  1. (Better) Don't add the letter in the first place (更好)不要把字母放在第一位

var a = function() { console.log(className + "a"); }

or或者

var a = function() {
  let temp = className + "a" ;
  console.log(temp);
}    
  1. Store the original value存储原始值

` `

 var className = ...;
 var backup = className;

 var a = function() {
   className = className + "a" ;
   console.log(className);
 }    
 console.log(className);
 a();
 className = backup;
 console.log(className);

` `

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

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