stampit API
Search…
Property descriptors
Property descriptors are standard JavaScript property descriptors. They are applied last thing when an object instance is being created. See more info in stamp internals.
1
const Key = stampit.props({
2
key: 'my secret key'
3
})
4
5
const auth = Key()
6
auth.key === 'my secret key'
7
auth.key = 'ha ha ha'
8
auth.key === 'ha ha ha' // oops :(
9
10
const FrozenKey = stampit.propertyDescriptors(Key, { // composing the two
11
key: { // this is a standard JavaScript property descriptor
12
configurable: false,
13
writable: false,
14
}
15
})
16
17
const auth = FrozenKey()
18
auth.key === 'my secret key'
19
auth.key = 'ha ha ha'
20
auth.key === 'my secret key' // OK!!! The "key" was not overwritten.
Copied!
The code above adds some more metadata to the Key stamp. It protects the key property from overwrites

Other ways to add property descriptors

Exactly the same stamp can be created in few ways. Here they all are.
1
const keyPropertyDescriptor = {
2
key: {
3
configurable: false,
4
writable: false,
5
}
6
}
7
8
const FrozenKey = stampit({
9
propertyDescriptors: keyPropertyDescriptor
10
})
11
12
const FrozenKey = stampit.propertyDescriptors(keyPropertyDescriptor)
13
14
const FrozenKey = stampit().propertyDescriptors(keyPropertyDescriptor)
Copied!
Last modified 3yr ago