Can/should objects share methods in certain circumstances?

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
}

Right?

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.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật