Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Andy Edinborough is correct about var <code>self != this</code>.</p> <p>It looks like you want to store a reference to the jQuery wrapper around your object instances, so I used the <a href="http://en.wikipedia.org/wiki/Flyweight_pattern" rel="nofollow">flyweight pattern</a> <em>(avoids creating a property on the object instance to store the jQuery wrapper)</em>. You can read more about this at James Padolsey's <a href="http://james.padolsey.com/javascript/76-bytes-for-faster-jquery/" rel="nofollow">blog</a>.</p> <p>I also cleaned up your code:</p> <pre><code>(function(window) { var flyweight = $([1]); /*--------------------------------------------------------------------------*/ function GrandParent() { /* empty constructor */ } function Parent() { /* empty constructor */ } function Child() { flyweight[0] = this; flyweight.bind('SomethingHappened', function(){ console.log('Client logs: SomethingHappened'); }) .bind('Happened', function(){ console.log('Client logs: Happened'); }); } /*--------------------------------------------------------------------------*/ // setup inheritance Parent.prototype = Object.create(GrandParent.prototype, { 'constructor': { 'configurable': true, 'writable': true, 'value': Parent } }); Child.prototype = Object.create(Parent.prototype, { 'constructor': { 'configurable': true, 'writable': true, 'value': Child } }); // add methods GrandParent.prototype.doFoo = function(){ flyweight[0] = this; flyweight.trigger('Happened'); }; Parent.prototype.doSomething = function(){ flyweight[0] = this; flyweight.trigger('SomethingHappened'); }; /*--------------------------------------------------------------------------*/ // expose window.MyApp = { 'GrandParent': GrandParent, 'Parent': Parent, 'Child': Child }; }(this)); </code></pre> <p>Usage check:</p> <pre><code>var foo = new MyApp.Child; var bar = new MyApp.Child; $(bar).bind('SomethingElse', function() { console.log('Client logs: SomethingElse'); }); foo.doFoo(); // Client logs: Happened foo.doSomething(); // Client logs: SomethingHappened $(bar).trigger('SomethingElse'); // Client logs: SomethingElse $(foo).trigger('SomethingElse'); // Nothing happens \o/ </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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