name
A very simple ES.next Private Name shim.
Using the private Name
object you can define hidden properties that no other code can access without the associated Name
object
This might be useful for storing meta data (like jQuery.data) or for creating a WeakMap shim
It works fairly well without any monkey-patching (though it does need to create one non-enumerable property on Object.prototype but it's much less intrusive than replacing functions. The variable name is random and uses unicode characters so the chances of conflicts are slim to none) This is the least intrusive and most accurate shim for private names that I know of, if you find a way to make it better feel free to make a pull request!
Installation
Install with component(1):
$ component install ilsken/name
Install with npm:
$ npm install --save private-name
Usage (with component or node.js)
var Name = // use `require('private-name')` for nodevar myObject = {} // you can also use new Name('something') to create a friendlier name for debugging tools which can see hidden propertiesvar hiddenProperty = // you can assign values directly using the hiddenProperty objectmyObjecthiddenProperty = 'foo' // but the property is automatically non-enumerable and the real name of the property is never exposed console // {} // you can still access the valueconsole // "foo" // but calls to hasOwnProperty and the like will fail (don't worry the property is still assigned to this object, not a prototype)console // false console // []
Usage in the browser (without component)
Gotchas
While this shim for the most part works without any monkey-patching there are currently a couple ways to leak the private properties names. If you want to fix those leaks you can use the patches below
Object.defineProperty
Because myHiddenName.toString()
doesn't return the actual name of the property but rather the name of our helper property on Object.prototype
, you will not be able to use Object.defineProperty
directly
You can use the following helper method instead
var hidden = hidden
Or you can apply the following patch
var def = ObjectdefineProperty{ ifprop instanceof Name return prop else return }ObjectdefineProperty = defineProperty
Object.getOwnPropertyNames(object)
Object.getOwnPropertyNames
will leak the private names of the variables. Because they use invisble unicode characters the chances of conflict are very slim but if you really want to make sure no one else can access the hidden variable names you can apply this patch
var props = ObjectgetOwnPropertyNames{ return }ObjectgetOwnPropertyNames = getOwnPropertyNames
Object.hasOwnProperty(Object.prototype, hiddenProperty)
If you define hidden properties on Object.prototype the method we use to hide doesn't work. I don't know why you would define hidden properties on the prototype but just in case here's how you'd patch this leak
var has = ObjecthasOwnProperty{ return Name && }ObjecthasOwnProperty = hasOwnProperty
License
MIT