Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong (or right) with this JS Object Pattern?
    primarykey
    data
    text
    <p>Here's an example of the pattern I'm using in my javascript objects these days (this example relies on jQuery). <a href="http://pastie.org/private/ryn0m1gnjsxdos9onsyxg" rel="nofollow noreferrer">http://pastie.org/private/ryn0m1gnjsxdos9onsyxg</a></p> <p>It works for me reasonably well, but I'm guessing there's something wrong, or at least sub-optimal about it, I'm just curious to get people's opinions.</p> <p>Here's a smaller, inline example of it:</p> <pre><code>sample = function(attach) { // set internal reference to self var self = this; // public variable(s) self.iAmPublic = true; // private variable(s) var debug = false; var host = attach; var pane = { element: false, display: false } // public function(s) self.show = function() { if (!pane.display) { position(); $(pane.element).show('fast'); pane.display = true; } } self.hide = function() { if (pane.display) { $(pane.element).hide('fast'); pane.display = false; } } // private function(s) function init () { // do whatever stuff is needed on instantiation of this object // like perhaps positioning a hidden div pane.element = document.createElement('div'); return self; } function position() { var h = { 'h': $(host).outerHeight(), 'w': $(host).outerWidth(), 'pos': $(host).offset() }; var p = { 'w': $(pane.element).outerWidth() }; $(pane.element).css({ top: h.pos.top + (h.h-1), left: h.pos.left + ((h.w - p.w) / 2) }); } function log () { if (debug) { console.log(arguments); } } // on-instantiation let's set ourselves up return init(); } </code></pre> <p>I'm really curious to get people's thoughts on this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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