I am currently working on a web application that is supposed to resemble an operating system with GUI (it manages processes and windows). I have several constructors which deal with different aspects of the program, such as Process and Window. There are a few built-in programs which use a special API and a utility function to extend Process and add certain methods to the prototype.

The reasoning for this is that there can be multiple running instances of the programs and I would like them to share the same methods. It would be pointless to define the very same method for each instance of a program (which is why I don't like the type of method that's commonly called a "privileged method" - it certainly is useful, but I consider it a bad approach).

The function mentioned above is in a namespace called Programs (all the programs are inside that namespace as well). This is what it looks like:

var Programs = {
    register: function(name, props) {
        function Program() {
            var args = Array.prototype.slice.call(arguments); // optional arguments
            args.unshift(name); // this will be used as the process name
            Process.apply(this, args);

        function Helper() {}
        Helper.prototype = Process.prototype;
        Program.prototype = new Helper();

        for (var k in props)
            Program.prototype[k] = props[k];
            // for example: Program.prototype.onStart = props.onStart;
            // the Process constructor is responsible for calling onStart()
            // for more information, look below

        return Program; // return the constructor

I create a program like so:

Programs.Terminal = Programs.register('terminal', {
    onStart: function() { // called when the process starts
        this.mainWindow = this.createWindow(null, 'Terminal'); // the createWindow() method (inherited from Process) takes two arguments, the parent window and the caption, and then returns the newly created window
    onCreate: function(window) { // called when a window is created with the reference to the window as the argument
        // add something to the window
    onActivate: function(window) { // called when a window gains focus
        // ...
    // and so on...

Now I can simply do new Programs.Terminal() to create a new instance of the Terminal program, and, since all the hooks (onStart(), onCreate() etc.) are added to the prototype, every instance of Terminal will share them.

Until now, one process could have exactly one window and everything worked fine, as I needed no "handle" to keep track of the windows. However, later I realized that it would be great if a process could have multiple windows, and this presented a problem.

Look at line 3 in the second code snippet:

this.mainWindow = this.createWindow(null, 'Terminal');

I grab the reference to the window and then, in theory, I can use it in the hooks in such a way:

onCreate: function(window) {
    if (window == this.mainWindow) // do something
    else if (window == this.someDialog) // do something else


No. The Process.createWindow() method, just as the name suggests, creates a new window, and then calls both the onCreate() and onActivate() hooks... before it returns! Before any assigment takes place! This way, this.mainWindow is not available yet when onCreate() and onActivate() are first called.

WinAPI employs a similar system - windows are created using the CreateWindow() (or CreateWindowEx()) function, that function returns a handle to the window, and later the handle can be used in the window procedure to determine which window the messages (e.g. WM_CREATE) came from. My guess is that this works, because when a new window is created, it doesn't immediately dispatch the message - instead, it stores the messages in a queue, and later (after the function returned and the handle has already been assigned to a variable) a loop polls the messages from the queue and dispatches them to the window procedure (feel free to correct me if I'm wrong). This certainly would not be an optimal solution for a web application.

I don't know why I had not thought of it before. Anyway, I was wondering how I could make it work and I came up with the following workaround (not really a solution, at least I don't find it as such): Process.createWindow() could take a "handle" argument (say, a number or a string), save it internally and then, e.g. in onCreate(), I could check window.handle in the following way:

onCreate: function(window) {
    if (window.handle == 'mainWindow') // do something
    else if (window.handle == 'someDialog') // do something else

However, I found that terrible and inelegant. I would like to manually assign the window reference to a property, such as this.mainWindow, push it onto an array, or store it in an object (so that I could control the scope).

What I think would work is something like the following:

Programs.Terminal = Programs.register('terminal', {
    onStart: function() {
        this.createWindow(null, 'Terminal', {
            onCreate: function() {
                // do something
            onActivate: function() {
                // do something else
            // etc.

Doesn't look too bad... but it would require the hook methods to be defined on each window object separately, which, I think, could affect performance (unless the browser performed some very radical optimizations) if there were a lot of windows present.

What do you think? Should I stick with the "string handle" workaround, should I go with the last proposed solution and ignore the fact that the same method is copied over and over, or perhaps something else? Or maybe my approach with extending Process is downright wrong? Thank you in advance.

  • I don't know enough about Javascript to formulate a code solution, but I do know that you need to be maintaining your open windows as objects, and registering your onCreate and onActivate functions with each object, independently. – Robert Harvey Nov 15 '14 at 18:09
  • It'd be correct and logical to define it on every one. You shouldn't worry about the performance of these things too much. – Zirak Nov 15 '14 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.