简体   繁体   中英

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;

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.

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.

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/

And in the example, if it's necessary, you can use the id property. Otherwise, if you keep track of the reference when calling sendRequest , you should be able to perform what you want on the correct 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. 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).

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

The very same can be used for ajax requests. 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. 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 ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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