I cannot thank you enough for your time and help! I've searched for almost 2 days and cannot find my exact answer. To begin:
I've always used object literal notation to create my objects. However, I've recently come across a situation where I need to create multiple instances of the same object. I believe what I'm attempting to create are "constructor functions":
I need to have the ability to create multiple "Window" objects:
var window1 = new Window();
var window2 = new Window();
var window3 = new Window();
I want the ability to be able to organize methods as such:
window1.errorMessage.show();
window2.errorMessage.hide();
window3.errorMessage.hide();
Instead of something like:
window1.showErrorMessage();
window2.hideErrorMessage();
window3.hideErrorMessage();
An example of how I would build my window object in literal notation:
var Window = {
id: null,
init : function(id) {
this.id = id;
},
errorMessage : {
show : function(message) {
// jquery that will simply take the id of this window,
// find the errorMessage html element within the window,
// insert the "message" and show it.
},
hide : function() {
// jquery that will simply take the id of this window,
// find the errorMessage html element within this window and hide it.
}
}
}
An example of how I would attempt to build my window object using constructor functions and prototyping:
function Window(id) {
this.id = id;
this.errorMessage = function() {}
}
Window.prototype.errorMessage = function() {}
Window.errorMessage.prototype.show = function(message) {
// jquery that will simply take the id of this window,
// find the errorMessage html element within the window,
// insert the "message" and show it.
}
Window.errorMessage.prototype.hide = function() {
// jquery that will simply take the id of this window,
// find the errorMessage html element within this window and hide it.
}
When I attempt to execute the following code:
var window1 = new Window();
window1.errorMessage.show('An error message');
(Ultimately I would like to call it using:)
this.errorMessage.show('An error message');
I receive the following console errors from Firefox:
- TypeError: Window.errorMessage is undefined
- TypeError: Window.errorMessage.show is not a function
Thank so much for you help. I appreciate it.