Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How should this be constructed so onclick events function as they do in the first method?</p> <p>Uncaught TypeError: Object # has no method 'addEventListener'</p> </blockquote> <p>That's obcously the reason. You need to add the event listenere to a DOM element, so you could do for example:</p> <pre><code>var clickMe = new CountMe(); document.getElementById("square").addEventListener("click", clickMe.countClick, false); </code></pre> <p>The variable <code>elt</code> is, as correctly commented, privately scoped and cannot be accessed from outside. That means you just should move the add-process into the constructor (if the element is determinable), or drop it at all.</p> <blockquote> <p>In the method countClick what context is the nested this in? The instance of the CountMe class I suspect, just want someone else's take.</p> </blockquote> <p>That depends on the calling. With your code</p> <pre><code>clickMe.countClick(); </code></pre> <p>the instance <code>clickMe</code> would be the context, but when the function is called as an event listener the DOM element will be the context. See MDN's overview for the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/this" rel="nofollow noreferrer"><code>this</code> keyword</a>. You can use a closure-scoped variable to hold a reference to the actual instance, as for example described <a href="https://stackoverflow.com/questions/1081499/accessing-an-objects-property-from-an-event-listener-call-in-javascript?lq=1">here</a> (there are many questions about that).</p> <p>Three possible quick-fixes:</p> <ul> <li><code>domel.addEventListener("click", clickMe.countClick.bind(clickMe), false)</code></li> <li><code>domel.addEventListner("click", function(){clickMe.countClick();}, false)</code></li> <li><code>var that=this; this.countClick=function(){ that.addCount(); alert(); };</code></li> </ul>
    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. 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