Files
cell/docs/library/object.md

115 lines
1.9 KiB
Markdown

---
title: "object"
description: "Object creation and manipulation"
weight: 40
type: "docs"
---
The `object` function is an intrinsic (always available, no `use()` needed). It is **polymorphic** — its behavior depends on the types of its arguments.
## From a Record
### 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}
```
## From an Array of Keys
### 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() { print("...") }}
var dog = meme(animal)
dog.speak = function() { print("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"]
```