简体   繁体   English

创建实例动态javascript

[英]creating instances dynamic javascript

I have a function with some methods. 我有一些方法的功能。

function acpwindow(){
   this.gui=function(){
            //some stuff
          }
   this.update=function(){
             //some stuff    
          }

}

Now i would like to create multiple instances of that function. 现在我想创建该函数的多个实例。

i have a button that create windows. 我有一个创建窗口的按钮。 onclick new window will trigger; onclick新窗口将触发;

function createwindow(){
     var object1= new acpwindow();
/*
**Here is a problem I have, How to maintain the objects unique.**
*/
}

When user makes some actions on windows gui those requests sent to server and then server will respond for those request. 当用户在windows gui上执行某些操作时,那些发送到服务器然后服务器的请求将响应这些请求。

Now my other issue is how to how to update particular window according to the response. 现在我的另一个问题是如何根据响应更新特定窗口。

The only hope i have is I will generate a unique UUID for each request and the same will return in the response. 我唯一的希望是我将为每个请求生成一个唯一的UUID,并在响应中返回相同的UUID。

I think if you create some kind of window manager to manage the windows you create, it might be easier to send and process requests. 我认为如果您创建某种窗口管理器来管理您创建的窗口,则发送和处理请求可能更容易。 Something like: 就像是:

http://jsfiddle.net/v3T94/1/ http://jsfiddle.net/v3T94/1/

And in the example, if it's necessary, you can use the id property. 在示例中,如果有必要,您可以使用id属性。 Otherwise, if you keep track of the reference when calling sendRequest , you should be able to perform what you want on the correct acpwindow 否则,如果您在调用sendRequest时跟踪引用,您应该能够在正确的acpwindow上执行您想要的acpwindow

The standard way to keep this kind of connection is using closures . 保持这种连接的标准方法是使用闭包

For example if you write 例如,如果你写

function make_timer()
{
    var x = document.createElement("div");
    var count = 0;
    setInterval(function(){
        count += 1;
        x.textContent = count;
    }, 1000);
    return x;
}

every call to make_timer will create an independent DOM node in which the content every second will be incremented. 每次调用make_timer都会创建一个独立的DOM节点,其中每秒的内容将递增。 But how can the timer callback remember which is the node that needs to be incremented? 但是,定时器回调如何记住哪个节点需要递增? The answer is that what is passed to setInterval indeed is not a function but a closure , ie a function plus some variables ( count and x in this case). 答案是传递给setInterval确实不是函数而是闭包 ,即函数加上一些变量(在本例中为countx )。

Languages like Java or C++ don't have this concept, but that happens is the the function that is created also it's said to "capture" local variables if they are from an outer scope and it will keep them "alive" even if the outer function that ceated them ends (ie when the function make_counter exits). 像Java或C ++这样的语言没有这个概念,但是发生的是创建的函数,如果它们来自外部范围,它也会被称为“捕获”局部变量,即使外部它也会使它们“活着”停止它们的函数结束(即当函数make_counter退出时)。

The very same can be used for ajax requests. 同样可以用于ajax请求。 What you normally do is just passing the window object to the function that submits the request and a callback closures will be used as completion and error callbacks. 你通常做的只是将window对象传递给提交请求的函数,回调闭包将用作完成和错误回调。 The closures will have access to the respective window objects when the answers come back from the server. 当答案从服务器返回时,闭包将访问相应的窗口对象。

Edit 编辑

If you really want to use ID s then of course you can... in your example they are store in an array so the array must be traversed to look for the exact ID ... 如果你真的想使用ID那么当然你可以...在你的例子中它们存储在一个数组中,因此必须遍历数组才能找到确切的ID ...

var windowid=$("#guiid").val();
for (var i=0; i<window_manager.windows.length; i++)
    if (window_manager.windows[i].id == windowid)
        window_manager.windows[i].gui();

Using an object instead of an array would be better because in that case the search could be reduced to a single like: 使用对象而不是数组会更好,因为在这种情况下,搜索可以简化为单个:

var windowid=$("#guiid").val();
window_manager.windows[windowid].gui();

Note however that in many cases numeric IDs for are not needed in Javascript because where you would store the window id you could store instead the reference to the window object itself , and for callbacks there is no need of complex machinery for providing context (like it's necessary in C++ or Java) because you have closures. 但请注意,在许多情况下,Javascript中不需要数字ID,因为在那里存储窗口ID,您可以存储窗口对象本身的引用 ,而对于回调,不需要复杂的机制来提供上下文(就像它的因为你有闭包,所以在C ++或Java中是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM