If a javascript variable lives outside the class, but within the closure of the module, is it private?

Bob

I came across the following code from https://facebook.github.io/flux/docs/todo-list.html#content, and had this question, since the website is declaring that

This object (_todos) contains all the individual to-do items. Because this variable lives outside the class, but within the closure of the module, it remains private — it cannot be directly changed from outside of the module.

Is this true? As far as I could know, it seems that the _todos is a global object.

var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var TodoConstants = require('../constants/TodoConstants');
var assign = require('object-assign');

var CHANGE_EVENT = 'change';

var _todos = {}; // collection of todo items

/**
 * Create a TODO item.
 * @param {string} text The content of the TODO
 */
function create(text) {
  // Using the current timestamp in place of a real id.
  var id = Date.now();
  _todos[id] = {
    id: id,
    complete: false,
    text: text
  };
}

/**
 * Delete a TODO item.
 * @param {string} id
 */
function destroy(id) {
  delete _todos[id];
}

var TodoStore = assign({}, EventEmitter.prototype, {

  /**
   * Get the entire collection of TODOs.
   * @return {object}
   */
  getAll: function() {
    return _todos;
  },

  emitChange: function() {
    this.emit(CHANGE_EVENT);
  },

  /**
   * @param {function} callback
   */
  addChangeListener: function(callback) {
    this.on(CHANGE_EVENT, callback);
  },

  /**
   * @param {function} callback
   */
  removeChangeListener: function(callback) {
    this.removeListener(CHANGE_EVENT, callback);
  },

      dispatcherIndex: AppDispatcher.register(function(payload) {
        var action = payload.action;
        var text;

        switch(action.actionType) {
          case TodoConstants.TODO_CREATE:
            text = action.text.trim();
            if (text !== '') {
              create(text);
              TodoStore.emitChange();
            }
            break;

          case TodoConstants.TODO_DESTROY:
            destroy(action.id);
            TodoStore.emitChange();
            break;

          // add more cases for other actionTypes, like TODO_UPDATE, etc.
        }

        return true; // No errors. Needed by promise in Dispatcher.
      })

})    ;

module.exports = TodoStore;
danillouz

Yes this is true.

In your example _todos is scoped to the module (i.e the file) itself and is NOT a global.

In node.js variables are scoped to the module. And it won't become a global (like on the browser). For reference see this question.

If you use something like browserify this is still true because from a top level perspective browserify uses an immediately invoked function expression to load in a mapping of dependencies (i.e modules) which are basically wrapped in a function that has it's own scope (NOT the global scope). More information on how that works can be found here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Grab variable outside from within closure

From Dev

Accesing private module variable from class

From Dev

Accesing private module variable from class

From Dev

Accessing Linked List private variable from outside class

From Dev

Accessing a class member from within an asynchronous closure in Javascript

From Dev

JavaScript closure within eventlistener

From Dev

Java reference variable declared outside method lives on stack or on heap

From Dev

How to change value of a private variable in a JavaScript method from outside?

From Dev

Export a class from a module where each instance has a private closure, but it's prototype chain can still be extended?

From Dev

php variable reference within closure

From Dev

Nodejs closure variable not updated in module

From Dev

Life of variable in Javascript Closure

From Dev

Javascript class library in closure

From Dev

How to access a variable of a class created within another class, from outside either class

From Dev

Why does my Javascript snippet for a module return undefined on a private variable?

From Dev

javascript module using closure and a function

From Dev

How to increment a variable contained within a class in python from outside of its class?

From Dev

php class private property access outside class

From Dev

Value not available within a private class

From Dev

private prototype from within a class

From Dev

private prototype from within a class

From Dev

Private members of class - within this context

From Dev

Value not available within a private class

From Dev

How to get and set private instance variable/ property from outside a class Objective C?

From Dev

How to get and set private instance variable/ property from outside a class Objective C?

From Dev

Accessing private class variable

From Dev

How can I move a captured variable into a closure within a closure?

From Dev

Swift - return variable from within closure

From Dev

using closure to make private properties javascript

Related Related

  1. 1

    Grab variable outside from within closure

  2. 2

    Accesing private module variable from class

  3. 3

    Accesing private module variable from class

  4. 4

    Accessing Linked List private variable from outside class

  5. 5

    Accessing a class member from within an asynchronous closure in Javascript

  6. 6

    JavaScript closure within eventlistener

  7. 7

    Java reference variable declared outside method lives on stack or on heap

  8. 8

    How to change value of a private variable in a JavaScript method from outside?

  9. 9

    Export a class from a module where each instance has a private closure, but it's prototype chain can still be extended?

  10. 10

    php variable reference within closure

  11. 11

    Nodejs closure variable not updated in module

  12. 12

    Life of variable in Javascript Closure

  13. 13

    Javascript class library in closure

  14. 14

    How to access a variable of a class created within another class, from outside either class

  15. 15

    Why does my Javascript snippet for a module return undefined on a private variable?

  16. 16

    javascript module using closure and a function

  17. 17

    How to increment a variable contained within a class in python from outside of its class?

  18. 18

    php class private property access outside class

  19. 19

    Value not available within a private class

  20. 20

    private prototype from within a class

  21. 21

    private prototype from within a class

  22. 22

    Private members of class - within this context

  23. 23

    Value not available within a private class

  24. 24

    How to get and set private instance variable/ property from outside a class Objective C?

  25. 25

    How to get and set private instance variable/ property from outside a class Objective C?

  26. 26

    Accessing private class variable

  27. 27

    How can I move a captured variable into a closure within a closure?

  28. 28

    Swift - return variable from within closure

  29. 29

    using closure to make private properties javascript

HotTag

Archive