2

I am reading an article here, and wondering what does this snippet of code mean? Especially what does this kind of assignment do var _private = my._private = my._private || {}?

var MODULE = (function (my) {
    var _private = my._private = my._private || {},
        _seal = my._seal = my._seal || function () {
            delete my._private;
            delete my._seal;
            delete my._unseal;
        },
        _unseal = my._unseal = my._unseal || function () {
            my._private = _private;
            my._seal = _seal;
            my._unseal = _unseal;
        };

    // permanent access to _private, _seal, and _unseal

    return my;
}(MODULE || {}));
Blake
  • 7,367
  • 19
  • 54
  • 80

1 Answers1

2

You just need to break it down.

var _private = my._private = my._private || {}?

You have two parts.

The first one is:

my._private = my._private || {}?

Which is covered by this question.

The || operator resolves to the left hand side if the left hand side is true and the right hand side otherwise.

The second one is:

var _private = my._private = something

Which is covered by this question.

The value of something is assigned to my._private and then that new value of my._private is assigned to _private, which is a locally scoped variable because it has var.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335