# GenericUnit

**GenericUnit** is a type of [Unit](https://docs.activejs.dev/activejs/fundamentals/units) that doesn't pertain to any specific data type, it's generic, as the name suggests. You can use it to store any serializable value data type that other Units accept. i.e.: `boolean`, `number`, `string`, simple `object` and `array`.

The advantage of using **GenericUnit** over other **Units** is that you can store and `dispatch` any type of value and the disadvantage is that you lose the specialized methods and features built into other Units for a specific data structure, for example, we can store an `array` value in a **GenericUnit**, but we cannot use methods like `push` or `set` to add items to the value without a manual `dispatch`. Also, we lose the assurance that the value would always be of a specific data type, i.e.: an `array` in case of **ListUnit**.

GenericUnit implements non-proto [Object.prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) methods like `toString`&#x20;to make working with the stored value a bit easier, when you call these methods they are called on the stored value instead of GenericUnit instance.

It also borrows some static methods from [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) like `values` and `entries`, and implements them as instance members `objectValues` and `objectEntries`, respectively.  These methods do not throw an error on `undefined` or `null` value, they would simply return an empty array instead.

See [API reference](https://api.activejs.dev/classes/genericunit) for more details.

|                      |                                                               |
| -------------------- | ------------------------------------------------------------- |
| Default value        | `undefined`                                                   |
| Supported data types | `boolean`, `number`, `string`, serializable `object`, `array` |

```typescript
// initialization
const unit = new GenericUnit({initialValue: ['🐠']}); // value is ['🐠']

typeof unit === 'object' // true
unit instanceof Array // false
unit.value() instanceof Array // true

// adding an item to the stored array value
unit.dispatch(arrValue => [...arrValue, '🐞'])
// this is inefficient compared to ListUnit.push, but it works

// dispatching a different type of value
unit.dispatch('not an array') // works

// dispatching a different type of value
unit.dispatch(true) // works
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.activejs.dev/activejs/fundamentals/units/genericunit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
