I recognize that some Javascript Objects has some internal slot property surrounding with double square bracket [[ ]] and can only be accessed by a method.
For example:
var str = new String('example');
When inspecting variable str
, I see the property [[PrimitiveValue]] has the example value, but I cannot access this property because it's internal. The only way to access it by the method toString()
only.
My question is: how can I implement the custom object with that behavior?, having an internal property with [[ ]] surrounding and a method to access that property.
var SLOT = require('internal-slot');
var assert = require('assert');
var o = {};
assert.throws(function () { SLOT.assert(o, 'foo'); });
assert.equal(SLOT.has(o, 'foo'), false);
assert.equal(SLOT.get(o, 'foo'), undefined);
SLOT.set(o, 'foo', 42);
assert.equal(SLOT.has(o, 'foo'), true);
assert.equal(SLOT.get(o, 'foo'), 42);
assert.doesNotThrow(function () { SLOT.assert(o, 'foo'); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.