Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess inner function variables in Javascript
    primarykey
    data
    text
    <p>In many frameworks, internal function variables are used as private variables, for example</p> <pre><code>Raphael = (function(){ var _private = function(a,b) {return a+b;}; var _public = function(a) {return _private(a,a);} var object = {mult2:_public}; return object; })(); </code></pre> <p>here, we cannot access from the global namespace the variable named <code>private</code>, as it is an inner variable of the anonymous function in the first line.</p> <p>Sometimes this function is contains a big Javascript framework, so that it wouldn't pollute the global namespace.</p> <p>I need to unit tests some object <code>Raphael</code> uses internally (in the above example, I wish to run unit tests on the object <code>private</code>). How can I test them?</p> <p><strong>edit:</strong> I received comments about unit tests which are supposed to test public interfaces.</p> <p>Let me specify a use case. I'm writing a library called <code>Raphael</code>. This library is supposed to add only a single name to the global namespace, and nothing more. This is a peculiar requirement for Javascript, since Javascript does not have namespaces.</p> <p>Let's say <code>Raphael</code> uses a linked list. If Javascript had the notion of packages, I would do</p> <pre><code>require 'linked_list' Raphael = (function(){/* use linked list */})(); </code></pre> <p>However Javascript does not allow me to do that in any way that wouldn't pollute the global scope with the linked list object! I'm therefore bound to inline <code>linked_list</code> into Raphael's local scope:</p> <pre><code>Raphael = (function(){ /* implement linked list */ var linked_list = function(){/*implementation*/}; })(); </code></pre> <p>And now I want to test <code>linked_list</code> implementation.</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.
 

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