1. <legend id='dAEFe'><style id='dAEFe'><dir id='dAEFe'><q id='dAEFe'></q></dir></style></legend>

    <i id='dAEFe'><tr id='dAEFe'><dt id='dAEFe'><q id='dAEFe'><span id='dAEFe'><b id='dAEFe'><form id='dAEFe'><ins id='dAEFe'></ins><ul id='dAEFe'></ul><sub id='dAEFe'></sub></form><legend id='dAEFe'></legend><bdo id='dAEFe'><pre id='dAEFe'><center id='dAEFe'></center></pre></bdo></b><th id='dAEFe'></th></span></q></dt></tr></i><div id='dAEFe'><tfoot id='dAEFe'></tfoot><dl id='dAEFe'><fieldset id='dAEFe'></fieldset></dl></div>
  2. <small id='dAEFe'></small><noframes id='dAEFe'>

      <bdo id='dAEFe'></bdo><ul id='dAEFe'></ul>
  3. <tfoot id='dAEFe'></tfoot>

      如何将 javascript 函数存储在队列中以便最终执行

      How do I store javascript functions in a queue for them to be executed eventually(如何将 javascript 函数存储在队列中以便最终执行)
      • <bdo id='3zY5o'></bdo><ul id='3zY5o'></ul>

      • <i id='3zY5o'><tr id='3zY5o'><dt id='3zY5o'><q id='3zY5o'><span id='3zY5o'><b id='3zY5o'><form id='3zY5o'><ins id='3zY5o'></ins><ul id='3zY5o'></ul><sub id='3zY5o'></sub></form><legend id='3zY5o'></legend><bdo id='3zY5o'><pre id='3zY5o'><center id='3zY5o'></center></pre></bdo></b><th id='3zY5o'></th></span></q></dt></tr></i><div id='3zY5o'><tfoot id='3zY5o'></tfoot><dl id='3zY5o'><fieldset id='3zY5o'></fieldset></dl></div>

        1. <legend id='3zY5o'><style id='3zY5o'><dir id='3zY5o'><q id='3zY5o'></q></dir></style></legend>
          <tfoot id='3zY5o'></tfoot>

            <small id='3zY5o'></small><noframes id='3zY5o'>

                  <tbody id='3zY5o'></tbody>
              • 本文介绍了如何将 javascript 函数存储在队列中以便最终执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 javascript 中创建了一个 Queue 类,我想将函数作为数据存储在队列中.这样我就可以构建请求(函数调用)并在需要时响应它们(实际执行函数).

                I have created a Queue class in javascript and I would like to store functions as data in a queue. That way I can build up requests (function calls) and respond to them when I need to (actually executing the function).

                有没有办法将函数存储为数据,有点类似于

                Is there any way to store a function as data, somewhat similar to

                .setTimeout("doSomething()", 1000);
                

                除非是

                functionQueue.enqueue(doSomething());
                

                它将 doSomething() 存储为数据的位置,因此当我从队列中检索数据时,将执行该函数.

                Where it would store doSomething() as data so when I retrieve the data from the queue, the function would be executed.

                我猜我必须在引号中加上 doSomething() -> "doSomething()" 以及如何让它使用字符串调用函数,有人知道怎么做吗?

                I'm guessing I would have to have doSomething() in quotes -> "doSomething()" and some how make it call the function using a string, anyone know how that could be done?

                推荐答案

                所有函数实际上都是变量,因此将所有函数存储在数组中实际上非常容易(通过在没有 () 的情况下引用它们):

                All functions are actually variables, so it's actually pretty easy to store all your functions in array (by referencing them without the ()):

                // Create your functions, in a variety of manners...
                // (The second method is preferable, but I show the first for reference.)
                function fun1() { alert("Message 1"); };
                var fun2 = function() { alert("Message 2"); };
                
                // Create an array and append your functions to them
                var funqueue = [];
                funqueue.push(fun1);
                funqueue.push(fun2);
                
                // Remove and execute the first function on the queue
                (funqueue.shift())();
                

                如果您想将参数传递给您的函数,这会变得有点复杂,但是一旦您设置了执行此操作的框架,此后每次都变得容易.本质上,您要做的是创建一个包装函数,该函数在被调用时会触发具有特定上下文和参数集的预定义函数:

                This becomes a bit more complex if you want to pass parameters to your functions, but once you've setup the framework for doing this once it becomes easy every time thereafter. Essentially what you're going to do is create a wrapper function which, when invoked, fires off a predefined function with a particular context and parameter set:

                // Function wrapping code.
                // fn - reference to function.
                // context - what you want "this" to be.
                // params - array of parameters to pass to function.
                var wrapFunction = function(fn, context, params) {
                    return function() {
                        fn.apply(context, params);
                    };
                }
                

                现在我们已经有了一个用于包装的实用函数,让我们看看它是如何用于创建未来的函数调用的:

                Now that we've got a utility function for wrapping, let's see how it's used to create future invocations of functions:

                // Create my function to be wrapped
                var sayStuff = function(str) {
                    alert(str);
                }
                
                // Wrap the function.  Make sure that the params are an array.
                var fun1 = wrapFunction(sayStuff, this, ["Hello, world!"]);
                var fun2 = wrapFunction(sayStuff, this, ["Goodbye, cruel world!"]);
                
                // Create an array and append your functions to them
                var funqueue = [];
                funqueue.push(fun1);
                funqueue.push(fun2);
                
                // Remove and execute all items in the array
                while (funqueue.length > 0) {
                    (funqueue.shift())();   
                }
                

                可以通过允许包装器使用数组或一系列参数来改进此代码(但这样做会混淆我正在尝试制作的示例).

                This code could be improved by allowing the wrapper to either use an array or a series of arguments (but doing so would muddle up the example I'm trying to make).

                这篇关于如何将 javascript 函数存储在队列中以便最终执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                      <small id='bKhhq'></small><noframes id='bKhhq'>

                      <i id='bKhhq'><tr id='bKhhq'><dt id='bKhhq'><q id='bKhhq'><span id='bKhhq'><b id='bKhhq'><form id='bKhhq'><ins id='bKhhq'></ins><ul id='bKhhq'></ul><sub id='bKhhq'></sub></form><legend id='bKhhq'></legend><bdo id='bKhhq'><pre id='bKhhq'><center id='bKhhq'></center></pre></bdo></b><th id='bKhhq'></th></span></q></dt></tr></i><div id='bKhhq'><tfoot id='bKhhq'></tfoot><dl id='bKhhq'><fieldset id='bKhhq'></fieldset></dl></div>

                      <legend id='bKhhq'><style id='bKhhq'><dir id='bKhhq'><q id='bKhhq'></q></dir></style></legend>
                    1. <tfoot id='bKhhq'></tfoot>
                        <bdo id='bKhhq'></bdo><ul id='bKhhq'></ul>
                          <tbody id='bKhhq'></tbody>