Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your example, the global object receives the <code>fn</code>. It is the <code>window</code> object in case of browsers. That is because you are <em>calling</em> the function in place (effectively constructing a new scope) without specific context. On the end, your <code>module</code> object is just a reference to the <code>window</code> (because of <code>return this;</code>).</p> <h2>What is <code>this</code>?</h2> <p>In JavaScript, <code>this</code> is the current context, the object on which the function was called that particular time. It is <strong>not</strong> the "holder" of the function. You can always "steal" the method from other objects and <code>apply</code> (literally) it to your own.</p> <p>Assume you want to slice the <code>arguments</code> object for some reason. It looks just like an array, but it is NOT an array. <code>arguments.slice(2,4)</code> does not work (assuming ECMAScript &lt; 5). What to do?</p> <pre><code>Array.prototype.slice.apply(arguments, [2,4]); </code></pre> <p>You need to <em>steal</em> the <code>slice</code> function from the Array prototype, and use if on your arguments. Inside the <code>slice</code> call, the "this" is the arguments object that particular time.</p> <h2>How to construct a valid module?</h2> <p>Your job is to <code>return</code> the module object. You do not want do mess with the context. It is not relevant, as long as you are not applying the function directly on module object.</p> <p>The easiest solution is the simplest.</p> <pre><code>var module = (function() { // do something internally, great for "private" stuff // then return the "public" interface return { doSomething: function() { // do something }, introduce: function() { console.log(this); } }; })(); module.introduce(); // Object {doSomething: function, introduce: function} module.doSomething(); </code></pre> <p><strong>The other way.</strong></p> <p>Alternatively, you could use the <code>this</code> to do your job, using the apply, if you really want to.</p> <pre><code>var module = {}; (function(){ this.doSomething = function() { // do something }; this.introduce = function() { console.log(this); }; }).apply(module); module.introduce(); // Object {doSomething: function, introduce: function} module.doSomething(); </code></pre> <p>Note this is almost equal to the "new" call.</p> <p>There are more equally valid ways to do it, but the first presented one is frequently used and very clear. Anyway, everything really depends on your code conventions.</p>
    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.
    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