explicit typeof == "undefined" check vs just checking for its existance?
<p>assuming x is an object...  Is there any benefit of doing:</p>

<pre><code> if (typeof x.foo != "undefined")
</code></pre>

<p>vs. doing</p>

<pre><code> if (x.foo)
</code></pre>

<p>?</p>

<p>This question came up as I was reading this blog post:
<a href="http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/" rel="nofollow">http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/</a></p>

<p>In his example, he does:</p>

<pre><code>function EventTarget(){
  this._listeners = {};
}

EventTarget.prototype = {

  constructor: EventTarget,

  addListener: function(type, listener){
    if (typeof this._listeners[type] == "undefined"){
        this._listeners[type] = [];
    }

    this._listeners[type].push(listener);
</code></pre>

<p>In this case this._listeners[type] will never be anything except an array-- so is it not true that it would be cleaner in this case to just do</p>

<pre><code>addListener: function(type, listener){
    if (!this._listeners[type]){
        this._listeners[type] = [];
    }

    this._listeners[type].push(listener);
</code></pre>

<p>?</p>

<p>Also, as a side question, I don't get why he's doing:</p>

<pre><code>EventTarget.prototype = {

  constructor: EventTarget
</code></pre>

<p>Isn't the constructor by default already set to EventTarget ('this') when you call new EventTarget() ?</p>
