stampit API
  • Introduction
  • Essentials
    • What is a Stamp
    • Installation
    • Specification
      • Merging algorithm
      • Object creation internals
    • FAQ
  • API
    • Quick start
    • Basics
    • Methods
    • Properties
    • Deep properties
    • Initializers
    • Static properties
    • Static deep properties
    • Configuration
    • Deep configuration
    • Composers
    • Property descriptors
    • Static property descriptors
    • Name
  • Ecosystem
    • Ecosystem Overview
    • @stamp/collision
    • @stamp/required
    • @stamp/privatize
    • @stamp/named (DEPRECATED)
    • @stamp/instanceof
Powered by GitBook
On this page
  1. API

Property descriptors

PreviousComposersNextStatic property descriptors

Last updated 7 years ago

Property descriptors are . They are applied last thing when an object instance is being created. See more info in .

const Key = stampit.props({
  key: 'my secret key'
})

const auth = Key()
auth.key === 'my secret key'
auth.key = 'ha ha ha'
auth.key === 'ha ha ha' // oops :(

const FrozenKey = stampit.propertyDescriptors(Key, { // composing the two
  key: { // this is a standard JavaScript property descriptor
    configurable: false,
    writable: false,
  }
})

const auth = FrozenKey()
auth.key === 'my secret key'
auth.key = 'ha ha ha'
auth.key === 'my secret key' // OK!!! The "key" was not overwritten.

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.

const keyPropertyDescriptor = {
  key: {
    configurable: false,
    writable: false,
  }
}

const FrozenKey = stampit({
  propertyDescriptors: keyPropertyDescriptor
})

const FrozenKey = stampit.propertyDescriptors(keyPropertyDescriptor)

const FrozenKey = stampit().propertyDescriptors(keyPropertyDescriptor)
standard JavaScript property descriptors
stamp internals