如何在不设置全局变量的情况下确定是否调用了函数

时间:2021-07-08 00:40:57

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函数中检查该变量来轻松完成此操作,但我想知道如何在不设置全局变量的情况下执行此操作。我真的想要一个这样做的方法的例子。

5 个解决方案

#1


23  

You could add a property to the function:

您可以向函数添加属性:

function init() {
    init.called = true;
}

init();

if(init.called) {
    //stuff
}

#2


17  

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/

#3


2  

Why don't you just check to see if your draggables have a class of draggable on them?

你为什么不检查一下你的拖拉机是否有可拖动的类?

if ($('.mydiv').is('.draggable')) {
     //do something
}

#4


2  

Function.prototype.fired = false;

function myfunc() { 
    myfunc.fired = true; 
    // your stuff
};

console.log(myfunc.fired) // false

myfunc();

console.log(myfunc.fired) // true

#5


1  

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将失败。在我的例子中,我无条件地调用第二个init只是为了表明什么都不会发生,但当然只有存在才能调用它:

if (o.init) o.init();

http://jsfiddle.net/coreyog/Wd3Q2/

http://jsfiddle.net/coreyog/Wd3Q2/

#1


23  

You could add a property to the function:

您可以向函数添加属性:

function init() {
    init.called = true;
}

init();

if(init.called) {
    //stuff
}

#2


17  

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/

#3


2  

Why don't you just check to see if your draggables have a class of draggable on them?

你为什么不检查一下你的拖拉机是否有可拖动的类?

if ($('.mydiv').is('.draggable')) {
     //do something
}

#4


2  

Function.prototype.fired = false;

function myfunc() { 
    myfunc.fired = true; 
    // your stuff
};

console.log(myfunc.fired) // false

myfunc();

console.log(myfunc.fired) // true

#5


1  

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将失败。在我的例子中,我无条件地调用第二个init只是为了表明什么都不会发生,但当然只有存在才能调用它:

if (o.init) o.init();

http://jsfiddle.net/coreyog/Wd3Q2/

http://jsfiddle.net/coreyog/Wd3Q2/