jQuery - 重置变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20669849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - resetting a variable
提问by steve_o
I have a variable in a web page that gets set to a value and is of type string. When a button is clicked, it calls a function & at the end of the function, I would want to clean up anything that had been set previously. I have tried using:
我在网页中有一个变量,它被设置为一个值并且是字符串类型。当一个按钮被点击时,它会调用一个函数 & 在函数的末尾,我想清理之前设置的任何东西。我试过使用:
$.removeData(myVar);
but it doesn't do anything. Log statements before & after that statement show that it still has the value & type both before & after the above statement.
但它没有做任何事情。该语句之前和之后的日志语句表明它仍然具有上述语句之前和之后的值和类型。
Another unused variable has a value of undefined
and a type of undefined
. Both these variables happen to be global variables in the page.
另一个未使用的变量的值为 ,undefined
类型为undefined
。这两个变量恰好是页面中的全局变量。
How do you reset myVar
to its initial state? I know it ought to be simple, but I'm missing something. Would appreciate any help.
你如何重置myVar
到它的初始状态?我知道它应该很简单,但我错过了一些东西。将不胜感激任何帮助。
回答by Katana314
Well...several possible direct answers.
嗯...几个可能的直接答案。
myVar = undefined;
myVar = null;
delete window.myVar;
HOWEVER, I would question the logic of having the variable be global, but only be used in certain methods. Here's how it might be better to structure (with a random pseudo-example of adding up values from ajax):
但是,我会质疑将变量设为全局但仅用于某些方法的逻辑。以下是如何更好地构建(使用从 ajax 添加值的随机伪示例):
addAjaxButton.onClick(function() {
var counter = 0;
ajax(function(addition) {
counter += addition;
ajax(function(moreAdd) {
counter += moreAdd;
alert('Total is ' + counter);
});
});
});
In that case, counter doesn't need to be deleted - it will just go out of scope when all AJAX is complete.
在这种情况下,不需要删除 counter - 当所有 AJAX 完成时,它就会超出范围。
回答by Bic
Just assign a value to it. If the initial state was undefined, set it to null. You don't have to do anything fancy with jQuery to kill the value.
只需为其分配一个值。如果初始状态未定义,则将其设置为 null。你不必用 jQuery 做任何花哨的事情来杀死价值。
回答by crad
It sounds like your function is doing some "side effecty" things and you want to undo those things?
听起来您的函数正在做一些“副作用”的事情,而您想撤消这些事情?
This statement
这个说法
I would want to clean up anything that had been set previously.
我想清理之前设置的任何内容。
Is not the same as this statement
和这个说法不一样
How do you reset myVar to its initial state?
如何将 myVar 重置为其初始状态?
What you actually want to do is copy it to a locally scoped variable and not change the global. This code demonstrates using function parameters and making a copy of the global var locally.
您真正想要做的是将其复制到本地范围的变量而不是更改全局变量。此代码演示了如何使用函数参数并在本地复制全局变量。
var someGlobal = 1;
someMethod();
someOtherMethod(someGlobal);
function someMethod() {
var localVar = someGlobal;
localVar = 2;
}
function someOtherMethod(localVar) {
localVar = 3;
}
window.console.log(someGlobal);
回答by redlena
This would be a way to reset the value:
这将是一种重置值的方法:
$(myVar).val('');