JavaScript 中只能调用一次的函数 [英] Function in JavaScript that can be called only once

查看:25
本文介绍了JavaScript 中只能调用一次的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个只能执行一次的函数,在第一次之后的每次都不会执行.我从 C++ 和 Java 知道可以完成这项工作的静态变量,但我想知道是否有更优雅的方法来做到这一点?

I need to create a function which can be executed only once, in each time after the first it won't be executed. I know from C++ and Java about static variables that can do the work but I would like to know if there is a more elegant way to do this?

推荐答案

如果通过不会被执行"你的意思是多次调用什么都不做",你可以创建一个闭包:

If by "won't be executed" you mean "will do nothing when called more than once", you can create a closure:

var something = (function() {
    var executed = false;
    return function() {
        if (!executed) {
            executed = true;
            // do something
        }
    };
})();

something(); // "do something" happens
something(); // nothing happens

回答@Vladloffe 的评论(现已删除):使用全局变量,其他代码可以重置已执行"的值标志(无论您为它选择什么名称).使用闭包,其他代码无法做到这一点,无论是意外还是故意.

In answer to a comment by @Vladloffe (now deleted): With a global variable, other code could reset the value of the "executed" flag (whatever name you pick for it). With a closure, other code has no way to do that, either accidentally or deliberately.

正如这里的其他答案所指出的,几个库(例如 UnderscoreRamda) 有一个接受函数的小实用函数(通常命名为 once()[*])作为参数并返回另一个函数,该函数只调用一次提供的函数,而不管返回的函数被调用多少次.返回的函数还会缓存由提供的函数首先返回的值,并在后续调用中返回该值.

As other answers here point out, several libraries (such as Underscore and Ramda) have a little utility function (typically named once()[*]) that accepts a function as an argument and returns another function that calls the supplied function exactly once, regardless of how many times the returned function is called. The returned function also caches the value first returned by the supplied function and returns that on subsequent calls.

但是,如果您不使用这样的第三方库,但仍然想要一个实用程序函数(而不是我上面提供的 nonce 解决方案),那么它很容易实现.我见过的最好的版本是 David Walsh 发布的这个版本:

However, if you aren't using such a third-party library, but still want a utility function (rather than the nonce solution I offered above), it's easy enough to implement. The nicest version I've seen is this one posted by David Walsh:

function once(fn, context) { 
    var result;
    return function() { 
        if (fn) {
            result = fn.apply(context || this, arguments);
            fn = null;
        }
        return result;
    };
}

我倾向于将 fn = null; 更改为 fn = context = null;.一旦 fn 被调用,闭包就没有理由维护对 context 的引用.

I would be inclined to change fn = null; to fn = context = null;. There's no reason for the closure to maintain a reference to context once fn has been called.

用法:

function something() { /* do something */ }
var one_something = once(something);

one_something(); // "do something" happens
one_something(); // nothing happens


[*] 但是请注意,其他库,例如 这个针对 jQuery 的 Drupal 扩展,可能有一个名为 once() 的函数,它可以做一些完全不同的事情.


[*] Be aware, though, that other libraries, such as this Drupal extension to jQuery, may have a function named once() that does something quite different.

这篇关于JavaScript 中只能调用一次的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆