Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

ES.next private name objects shim

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 tests
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 History.md
Octocat-spinner-32 License.md
Octocat-spinner-32 Readme.md
Octocat-spinner-32 core.js
Octocat-spinner-32 package.json
Readme.md

name

Build Status

Polyfill of ES.next private name objects. This implementation patches ES5 Object methods where possible to emulate private names as much as possible. Still in some cases private names can be discovered:

  • If Object methods from other JS contexts are used.
  • If properties are simply set like shown below, private names will be revealed by for iteration:

    object[secret] = value
    

    It is recommended to use Object.defineProperty or Object.defineProperties for defining private named properties. Those methods will make sure to mark property as non-enumerable so that such propertes won't be reavealed.

Install

npm install name

Use

var Name = require('name')
var secret = new Name()
function MyClass(privateData) {
  // do not do `this[secret] = privateData` do following instead
  Object.defineProperty(this, secret, {
    value: privateData
  })
  this.name = 'my'
}
MyClass.prototype = {
  doStuff: function() {
    /*...*/ this[secret] /*...*/
  }
}

var my = new MyClass()
Object.keys(my) // => [ 'my' ]
Object.getOwnPropertyNames(my) // => [ 'my' ]

var names = []
for (var name in my) names.push(name)
names // => [ 'my' ]
Something went wrong with that request. Please try again.