Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Emulating ES.next private names in spidermonkey
/*jshint asi:true */
!(function(getOwnNames, getKeys, nil) {
function ignore() {}
function nonPrivate(name) { return name[0] !== '\\' }
Object.defineProperties(Object, {
getOwnPropertyNames: { value: (function(object) {
return getOwnNames(object).filter(nonPrivate)
}).bind(null) },
keys: { value: (function(object) {
return getKeys(object).filter(nonPrivate)
}).bind(null) }
})
Object.defineProperty(Object.prototype, '__iterator__', {
value: function iterator(names) {
let iteratee = Object.create(this, { '__iterator__': { value: nil } })
for (let name in iteratee) {
if (nonPrivate(name))
yield names ? name : iteratee[name]
}
}
})
})(Object.getOwnPropertyNames, Object.keys)
function Name() {
return Object.create(Name.prototype, {
toString: {
value: String.bind(null, '\\' + Math.round(Math.random() * 1000000000000))
}
})
}
function props(object) {
var result = []
for (let key in object) result.push(key)
return result
}
var bar = new Name()
var a = { foo: 'foo!' }
a[bar] = 'bar'
Object.keys(a)
Object.getOwnPropertyNames(a)
props(a)
var b = Object.create(a)
Object.keys(b)
Object.getOwnPropertyNames(b)
props(b)
@Gozala
Owner

Only issue with this approach is that if one creates Object.create(yourObject, { __iterator__: { value: undefined } }) he will be able to gain access to all private names and properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.