Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript sub methods / namespacing with constructor functions
    primarykey
    data
    text
    <p><strong>I cannot thank you enough for your time and help!</strong> I've searched for almost 2 days and cannot find my exact answer. To begin:</p> <p>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":</p> <p>I need to have the ability to create multiple "Window" objects:</p> <pre><code>var window1 = new Window(); var window2 = new Window(); var window3 = new Window(); </code></pre> <p>I want the ability to be able to organize methods as such:</p> <pre><code>window1.errorMessage.show(); window2.errorMessage.hide(); window3.errorMessage.hide(); </code></pre> <p>Instead of something like:</p> <pre><code>window1.showErrorMessage(); window2.hideErrorMessage(); window3.hideErrorMessage(); </code></pre> <p><strong>An example of how I would build my window object in literal notation:</strong></p> <pre><code>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. } } } </code></pre> <p><strong>An example of how I would attempt to build my window object using constructor functions and prototyping:</strong></p> <pre><code>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. } </code></pre> <p><strong>When I attempt to execute the following code:</strong></p> <pre><code>var window1 = new Window(); window1.errorMessage.show('An error message'); </code></pre> <p>(Ultimately I would like to call it using:)</p> <pre><code>this.errorMessage.show('An error message'); </code></pre> <p>I receive the following console errors from Firefox:</p> <ul> <li>TypeError: Window.errorMessage is undefined</li> <li>TypeError: Window.errorMessage.show is not a function</li> </ul> <p><br> <br> <br></p> <p><strong>Thank so much for you help. I appreciate it.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload