Javascript 如何在不设置全局变量的情况下确定函数是否已被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11299952/
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
How to determine if a function has been called without setting global variable
提问by codelove
I am looking for a good technique to get away from what I am tempted to do: to set a global variable.
我正在寻找一种好的技术来摆脱我想做的事情:设置全局变量。
The first time someone runs a function by clicking a button it triggers an initial function to turn a few things into draggables. Later, if they click the button a second time I want to determine if the init
function has been initialized, and if so to not call it again. I could easily do this by setting a global variable from the init
function and then checking that variable from the click function, but I'm wondering how to do this without setting a global variable. I would really like an example of a way to do this.
第一次有人通过单击按钮运行一个函数时,它会触发一个初始函数,将一些东西变成可拖动的。稍后,如果他们第二次单击该按钮,我想确定该init
函数是否已初始化,如果已初始化,则不再调用它。我可以通过从init
函数设置一个全局变量然后从 click 函数检查该变量来轻松做到这一点,但我想知道如何在不设置全局变量的情况下做到这一点。我真的很想要一个方法的例子。
回答by Levi Hackwith
You could add a property to the function:
您可以向该函数添加一个属性:
function init() {
init.called = true;
}
init();
if(init.called) {
//stuff
}
回答by Amith George
While @Levi's answer ought to work just fine, I would like to present another option. You would over write the init function to do nothing once it has been called.
虽然@Levi 的答案应该可以正常工作,但我想提出另一种选择。一旦调用了 init 函数,您就可以覆盖它什么也不做。
var init = function () {
// do the initializing
init = function() {
return false;
}
};
The function when called the first time will do the init. It will then immediately overwrite itself to return false the next time its called. The second time the function is called, the function body will only contain return false.
第一次调用时的函数将执行 init。然后它将立即覆盖自己以在下次调用时返回 false。第二次调用函数时,函数体将只包含 return false。
For more reading: http://www.ericfeminella.com/blog/2011/11/19/function-overwriting-in-javascript/
更多阅读:http: //www.ericfeminella.com/blog/2011/11/19/function-overwriting-in-javascript/
回答by Jason
Why don't you just check to see if your draggables have a class of draggable
on them?
你为什么不检查一下你的可拖动对象是否有一类draggable
呢?
if ($('.mydiv').is('.draggable')) {
//do something
}
回答by Cau
Function.prototype.fired = false;
function myfunc() {
myfunc.fired = true;
// your stuff
};
console.log(myfunc.fired) // false
myfunc();
console.log(myfunc.fired) // true
回答by Corey Ogburn
What you could do is unhook the init function from the prototype.
你可以做的是从原型中解开 init 函数。
?var Obj = function () {
this.init = function () {
document.write("init called<br/>");
this.init = null;
}
}
var o = new Obj();
if (o.init) document.write("exists!<br/>");
o.init();
if (o.init) document.write("exists!<br/>");
o.init();
?
The first if
will be true and print exists!
but since the function removes itself, the second if
will fail. In my example, I call the second init unconditionally just to show that nothing will happen, but of course you could call it only if it exists:
? 第一个if
将为 true 并打印,exists!
但由于该函数会自行删除,因此第二个if
将失败。在我的例子中,我无条件地调用第二个 init 只是为了表明什么都不会发生,但当然你只能在它存在时调用它:
if (o.init) o.init();