---
title: PREF
project: jComponent V19
version: 19.194
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/jcomponent-v19/19-194/reference/pref/
updated: 2026-11-18T13:58:24.464Z
---

# PREF

> Le preferenze utente memorizzate nel browser.

`PREF` memorizza nel browser piccoli valori con una scadenza. I valori sono pronti prima dell'avvio dei primi componenti. I path memorizzati possono essere letti anche con [GET()](../globals/#get), come spiegato in [Path](../../guides/paths/).

## API

### PREF.get(key, [expire])

Type: `method`

Legge un valore memorizzato.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | La chiave. |
| `expire` | `String` | no |  | Memorizza di nuovo il valore con questa scadenza. |

Returns: `Object` Il valore, oppure `undefined` quando non esiste o è scaduto.

Un valore memorizzato è disponibile anche come proprietà, ad esempio `PREF.theme`.

Example: Leggi un valore
```js
PREF.set('theme', 'dark');
console.log(PREF.get('theme')); // dark
console.log(PREF.theme); // dark
```

### PREF.set(key, [value], [expire])

Type: `method`

Memorizza un valore.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | La chiave. |
| `value` | `Object` | no |  | Il valore. |
| `expire` | `String` | no | `1 month` | Ad esempio `1 day`. |

Returns: `Object` Il valore.

Senza un valore, la chiave viene rimossa. Gli osservatori e i componenti sul path `PREF.key` vengono notificati. I dati vengono salvati dopo `DEF.delaypref` millisecondi. Le chiavi `get`, `set`, `keys`, `load` e `save` non sono disponibili come proprietà.

Example: Memorizza e osserva un valore
```js
WATCH('PREF.theme', function(path, value) {
	console.log(value); // dark
});
PREF.set('theme', 'dark', '1 day');
```

Example: Rimuovi un valore
```js
PREF.set('theme', 'dark');
PREF.set('theme');
console.log(PREF.theme === undefined); // true
```

### PREF.keys()

Type: `method`

Elenca le chiavi memorizzate.

Returns: `Array` Le chiavi.

Example: Elenca le chiavi
```js
PREF.set('theme', 'dark');
console.log(PREF.keys()); // ["theme"]
```

### PREF.load(callback)

Type: `method`

Legge i dati memorizzati all'avvio della pagina.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | yes |  | `function(data)`. |

Sostituisci questa funzione per leggere i dati da un'altra posizione. Passa i dati alla callback come oggetto in cui ogni chiave contiene `{ value, expire }`.

Example: Carica da una tua origine
```js
PREF.load = function(callback) {
	callback({ theme: { value: 'dark', expire: NOW.add('1 day') } });
};
setTimeout(function() {
	console.log(PREF.theme); // dark
}, 500);
```

### PREF.save(data)

Type: `method`

Scrive i dati memorizzati.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object` | yes |  | I dati da salvare. |

Sostituisci questa funzione per salvare i dati in un'altra posizione. Per impostazione predefinita, i dati finiscono nell'archiviazione del browser sotto `DEF.localstorage` seguito da `.pref`.

Example: Salva in una tua destinazione
```js
DEF.delaypref = 50;
PREF.save = function(data) {
	console.log(data.theme.value); // dark
};
PREF.set('theme', 'dark');
```
