stampit API
Search…
Object creation internals
This is a plain factory function. It creates and returns a plain object.
1
function myFactory () {
2
return { myProperty: 'my value' }
3
}
Copied!
But Stamps are a little different. They carry around the metadata about the object they are going to produce. The metadata is attached to the Stamp.compose object:
1
function Stamp () {
2
const metadata = Stamp.compose // retrieving metadata from itself
3
4
... SNIP ...
5
6
return newObject
7
}
Copied!
There are 11 standardized types of metadata, such as properties and deepProperties.
1
function Stamp () {
2
const metadata = Stamp.compose // retrieving metadata from itself
3
4
const properties = metadata.properties // retrieving properties from the metadata
5
let deepProperties = metadata.deepProperties // retrieving deepProperties from the metadata
6
deepProperties = JSON.parse(JSON.stringify(deepProperties)) // we need to clone deepProperties
7
const newObject = Object.assign({}, deepProperties, properties) // creating new object using the properties
8
9
... SNIP ...
10
11
return newObject
12
}
Copied!
There is also methods object which becomes the new object's prototype.
1
function Stamp () {
2
const metadata = Stamp.compose // retrieving metadata from itself
3
4
const properties = metadata.properties // retrieving properties from the metadata
5
let deepProperties = metadata.deepProperties // retrieving deepProperties from the metadata
6
deepProperties = _.cloneDeep(deepProperties) // we need to clone deepProperties
7
const newObject = Object.assign({}, deepProperties, properties) // creating new object using the properties
8
9
const methods = metadata.methods // retrieving methods from the metadata
10
if (methods) Object.setPrototypeOf(newObject, methods) // assigning new object's prototype
11
12
... SNIP ...
13
14
return newObject
15
}
Copied!
The propertyDescriptors metadata is an object applied to the resulting object using Object.defineProperties JavaScript standard function.
1
function Stamp () {
2
const metadata = Stamp.compose // retrieving metadata from itself
3
4
const properties = metadata.properties // retrieving properties from the metadata
5
let deepProperties = metadata.deepProperties // retrieving deepProperties from the metadata
6
deepProperties = JSON.parse(JSON.stringify(deepProperties)) // we need to clone deepProperties
7
const newObject = Object.assign({}, deepProperties, properties) // creating new object using the properties
8
9
const methods = metadata.methods // retrieving methods from the metadata
10
if (methods) Object.setPrototypeOf(newObject, methods) // assigning new object's prototype
11
12
Object.defineProperties(newObject, metadata.propertyDescriptors)
13
14
... SNIP ...
15
16
return newObject
17
}
Copied!
Last, but not least, the initializers are kind of constructors. With classic classes you would execute only one constructor when creating an object. With stamps you execute all the initializers of a stamp.
1
function Stamp () {
2
const metadata = Stamp.compose // retrieving metadata from itself
3
4
const properties = metadata.properties // retrieving properties from the metadata
5
let deepProperties = metadata.deepProperties // retrieving deepProperties from the metadata
6
deepProperties = JSON.parse(JSON.stringify(deepProperties)) // we need to clone deepProperties
7
const newObject = Object.assign({}, deepProperties, properties) // creating new object using the properties
8
9
const methods = metadata.methods // retrieving methods from the metadata
10
if (methods) Object.setPrototypeOf(newObject, methods) // assigning new object's prototype
11
12
Object.defineProperties(newObject, metadata.propertyDescriptors)
13
14
for (const init of metadata.initializers || []) { // run every initializer one by one
15
const firstArg = argument[0]
16
const secondArg = { args: [...arguments], instance: newObject, stamp: Stamp }
17
const returnedValue = init.call(newObject, firstArg, secondArg)
18
if (returnedValue !== undefined) {
19
newObject = returnedValue
20
}
21
}
22
23
return newObject
24
}
Copied!
If an initializer returns a non-undefined value then it becomes the new object instance.
See Stamp Specification for more details.
NOTE
The code above is not the actual stampit code. It was optimized for readability purposes.
Last modified 3yr ago
Copy link