Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>&gt; $ = function() { &gt; this.name = "levi"; &gt; &gt; return this; }; &gt; &gt; console.log($()); </code></pre> <blockquote> <p>We get DOMWindow instead of $</p> </blockquote> <p>When you call <em>$</em> as a funciton, then its <em>this</em> keyword is set to the global object, as it would be for any function called like that.</p> <blockquote> <p>because of the quirky nature of this in JavaScript</p> </blockquote> <p>Javascript's <em>this</em> keyword* works as it is specified to work. It is different to other languages, but that is how it works.</p> <blockquote> <p>What's more strange to me is that console.log($().name) properly returns "levi".</p> </blockquote> <p>When you call <em>$</em> as a function, its <em>this</em> keyword is the global object, so:</p> <pre><code>this.name = 'levi'; </code></pre> <p>creates a property of the global object called <em>name</em> with a value of 'levi'. Not strange when you know what's happening. :-)</p> <blockquote> <p>We could just add new console.log(new $()) and it works.</p> </blockquote> <p>That is how constructors are supposed to be called in javascript. When a function is called called with <em>new</em>, its <em>this</em> keyword is set to a newly constructed object, so <code>this.name</code> will create a new property of that object. Incidentally, <code>return this</code> is redundant, constructors return <em>this</em> by default.</p> <pre><code>&gt; $ = function() { &gt; var obj = function() { &gt; this.name = "levi"; &gt; }; &gt; &gt; return new obj(); }; </code></pre> <blockquote> <p>console.log($()); Which gives me what I want, but it seems a bit unnecessary to wrap the object inside of a function which creates it. Further more, it's of type obj and not type $</p> </blockquote> <p>Presumably you are using <em>typeof</em>, which can only return one of the values specified by ECMA-262. That short list (which includes object, number, string an so on) does not include <em>$</em>.</p> <blockquote> <p>What are some other ways this can be done?</p> </blockquote> <p>You can use the approach you already have discovered, Lasse Reichstein Nielsen's clone (also popularised by Doublas Crockford as "beget") and Richard Cornford's module pattern. Use Google, there are many, many posts about all of the above.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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