如何将JavaScript函数存储在队列中以便最终执行它们 [英] How do I store javascript functions in a queue for them to be executed eventually

查看:108
本文介绍了如何将JavaScript函数存储在队列中以便最终执行它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

有什么方法可以将函数存储为数据,有点类似于/ b

  .setTimeout(doSomething(),1000); 

除了它是

  functionQueue.enqueue(doSomething的()); 

它会将doSomething()存储为数据,因此当我从队列中检索数据时,函数



我猜我必须在引号中使用doSomething() - >doSomething(),以及如何使用函数调用函数字符串,任何人都知道如何做到这一点?

解决方案

所有函数实际上都是变量,所以它实际上很容易存储所有函数在数组中(通过引用它们而没有()):

  //创建你的函数,以各种方式... 
//(第二种方法更可取,但我首先参考)
函数fun1(){alert(Message 1 ); };
var fun2 = function(){alert(Message 2); };

//创建一个数组并将函数追加到它们
var funqueue = [];
funqueue.push(fun1);
funqueue.push(fun2);

//删除并执行队列中的第一个函数
(funqueue.shift())();

如果要将参数传递给函数,这会变得更复杂一些,但是一旦在此后每次都变得简单时,为此设置框架。从本质上讲,你要做的是创建一个包装函数,该函数在被调用时使用特定的上下文和参数集来触发预定义的函数:

  //函数包装代码。 
// fn - 对函数的引用。
//上下文 - 你想要这个是什么。
// params - 传递给函数的参数数组。
var wrapFunction = function(fn,context,params){
return function(){
fn.apply(context,params);
};

$ / code>

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

$ $ p $ //创建要包装的函数
var sayStuff = function(str ){
alert(str);
}

//包装该功能。确保参数是一个数组。
var fun1 = wrapFunction(sayStuff,this,[Hello,world!]);
var fun2 = wrapFunction(sayStuff,this,[Goodbye,cruel world!]);

//创建一个数组并将函数追加到它们
var funqueue = [];
funqueue.push(fun1);
funqueue.push(fun2);

//移除并执行数组
中的所有项目(funqueue.length> 0){
(funqueue.shift())();
}

这个代码可以通过允许包装器使用数组或者一系列的论点(但这样做会混淆我试图做的例子)。


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);

except it would be

functionQueue.enqueue(doSomething());

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

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函数存储在队列中以便最终执行它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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