Files
cell/docs/library/object.md
2025-12-17 02:53:01 -06:00

113 lines
1.8 KiB
Markdown

# object
The `object` function and related utilities handle object creation and manipulation.
## Creation
### object(obj)
Shallow copy an object.
```javascript
var copy = object(original)
```
### object(obj, another)
Combine two objects.
```javascript
object({a: 1}, {b: 2}) // {a: 1, b: 2}
object({a: 1}, {a: 2}) // {a: 2}
```
### object(obj, keys)
Select specific keys.
```javascript
object({a: 1, b: 2, c: 3}, ["a", "c"]) // {a: 1, c: 3}
```
### object(keys)
Create object from keys (values are `true`).
```javascript
object(["a", "b", "c"]) // {a: true, b: true, c: true}
```
### object(keys, value)
Create object from keys with specified value.
```javascript
object(["a", "b"], 0) // {a: 0, b: 0}
```
### object(keys, fn)
Create object from keys with computed values.
```javascript
object(["a", "b", "c"], (k, i) => i) // {a: 0, b: 1, c: 2}
```
## Prototypes
### meme(prototype)
Create a new object with the given prototype.
```javascript
var animal = {speak: function() { log.console("...") }}
var dog = meme(animal)
dog.speak = function() { log.console("woof") }
```
### proto(obj)
Get an object's prototype.
```javascript
var p = proto(dog) // animal
```
### isa(obj, prototype)
Check if prototype is in the chain.
```javascript
isa(dog, animal) // true
```
## Serialization
### splat(obj)
Flatten an object's prototype chain into a plain object. Only includes primitive types (numbers, text, booleans, arrays, objects).
```javascript
var base = {x: 1}
var derived = meme(base)
derived.y = 2
splat(derived) // {x: 1, y: 2}
```
When sending objects between actors with `$send`, they are automatically splatted.
## Key Iteration
```javascript
var obj = {a: 1, b: 2, c: 3}
// Get all keys
var keys = array(obj) // ["a", "b", "c"]
// Iterate
for (var key in obj) {
log.console(key, obj[key])
}
```