From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- files/it/web/javascript/data_structures/index.html | 444 +++++++++++++++++++++ 1 file changed, 444 insertions(+) create mode 100644 files/it/web/javascript/data_structures/index.html (limited to 'files/it/web/javascript/data_structures') diff --git a/files/it/web/javascript/data_structures/index.html b/files/it/web/javascript/data_structures/index.html new file mode 100644 index 0000000000..a056282b0d --- /dev/null +++ b/files/it/web/javascript/data_structures/index.html @@ -0,0 +1,444 @@ +--- +title: Tipi di dato e strutture in JavaScript +slug: Web/JavaScript/Data_structures +tags: + - Guide + - JavaScript + - Principianti + - tipi +translation_of: Web/JavaScript/Data_structures +--- +
{{jsSidebar("More")}}
+ +

Tutti i linguaggi di programmazione utilizzano strutture di dati, ma queste possono spesso differire da un linguaggio all'altro. L'intento di questo articolo è elencare tutte le strutture di dati di JavaScript e le relative proprietà, che possono essere utilizzate anche per costruire nuove strutture di dati. Dove possible, compareremo le strutture di dati di JavaScript con quelle di altri linguaggi.

+ +

Tipi dinamici

+ +

JavaScript è un linguaggio di tipo debole o dinamico. Questo vuol dire che in JavaScript le variabili non sono direttamente associate a nessun particolare tipo e che una variabile può essere assegnata (e ri-assegnata) a qualsiasi tipo di valore:

+ +
let foo = 42;    // foo è ora Number
+foo     = 'bar'; // foo è ora String
+foo     = true;  // foo è ora Boolean
+
+ +

Tipi di dati

+ +

L'ultima definizione dello standard ECMAScript comprende nove tipi:

+ + + +

Keep in mind the only valuable purpose of typeof operator usage is checking the Data Type. If we wish to check any Structural Type derived from Object it is pointless to use typeof for that, as we will always receive "object". The indeed proper way to check what sort of Object we are using an instanceof keyword. But even in that case there might be misconceptions.

+ +

Valori primitivi

+ +

All types except objects define immutable values (that is, values which can't be changed). For example (and unlike in C), Strings are immutable. We refer to values of these types as "primitive values".

+ +

Tipo Boolean

+ +

Boolean represents a logical entity and can have two values: true and false. See Boolean and {{jsxref("Boolean")}} for more details.

+ +

Tipo Null

+ +

The Null type has exactly one value: null. See {{jsxref("null")}} and Null for more details.

+ +

Tipo Undefined

+ +

A variable that has not been assigned a value has the value undefined. See {{jsxref("undefined")}} and Undefined for more details.

+ +

Tipo Number

+ +

ECMAScript has two built-in numeric types: Number and BigInt (see below).

+ +

The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(253 − 1) and 253 − 1). In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and {{jsxref("NaN")}} ("Not a Number").

+ +

To check for the largest available value or smallest available value within {{jsxref("Infinity", "±Infinity")}}, you can use the constants {{jsxref("Number.MAX_VALUE")}} or {{jsxref("Number.MIN_VALUE")}}.

+ +

Starting with ECMAScript 2015, you are also able to check if a number is in the double-precision floating-point number range using {{jsxref("Number.isSafeInteger()")}} as well as {{jsxref("Number.MAX_SAFE_INTEGER")}} and {{jsxref("Number.MIN_SAFE_INTEGER")}}. Beyond this range, integers in JavaScript are not safe anymore and will be a double-precision floating point approximation of the value.

+ +

The number type has only one integer with two representations: 0 is represented as both -0 and +0. (0 is an alias for +0.) 

+ +

In the praxis, this has almost no impact. For example, +0 === -0 is true. However, you are able to notice this when you divide by zero:

+ +
> 42 / +0
+Infinity
+> 42 / -0
+-Infinity
+
+ +

Although a number often represents only its value, JavaScript provides {{jsxref("Operators/Bitwise_Operators", "binary (bitwise) operators")}}.

+ +

These bitwise operators can be used to represent several Boolean values within a single number using bit masking. However, this is usually considered a bad practice, since JavaScript offers other means to represent a set of Booleans (like an array of Booleans, or an object with Boolean values assigned to named properties). Bit masking also tends to make the code more difficult to read, understand, and maintain.

+ +

It may be necessary to use such techniques in very constrained environments, like when trying to cope with the limitations of local storage, or in extreme cases (such as when each bit over the network counts). This technique should only be considered when it is the last measure that can be taken to optimize size.

+ +

BigInt type

+ +

The {{jsxref("BigInt")}} type is a numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit for Numbers.

+ +

A BigInt is created by appending n to the end of an integer or by calling the constructor.

+ +

You can obtain the safest value that can be incremented with Numbers by using the constant {{jsxref("Number.MAX_SAFE_INTEGER")}}. With the introduction of BigInts, you can operate with numbers beyond the {{jsxref("Number.MAX_SAFE_INTEGER")}}.

+ +

This example demonstrates, where incrementing the {{jsxref("Number.MAX_SAFE_INTEGER")}} returns the expected result:

+ +
> const x = 2n ** 53n;
+9007199254740992n
+> const y = x + 1n;
+9007199254740993n
+
+ +

You can use the operators +, *, -, **, and % with BigInts—just like with Numbers. A BigInt is not strictly equal to a Number, but it is loosely so.

+ +

A BigInt behaves like a Number in cases where it is converted to Boolean: if, ||, &&, Boolean, !.

+ +

BigInts cannot be operated on interchangeably with Numbers. Instead a {{jsxref("TypeError")}} will be thrown.

+ +

String type

+ +

JavaScript's {{jsxref("String")}} type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values. Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it.

+ +

Unlike some programming languages (such as C), JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it.

+ +

However, it is still possible to create another string based on an operation on the original string. For example:

+ + + +

Beware of "stringly-typing" your code!

+ +

It can be tempting to use strings to represent complex data. Doing this comes with short-term benefits:

+ + + +

With conventions, it is possible to represent any data structure in a string. This does not make it a good idea. For instance, with a separator, one could emulate a list (while a JavaScript array would be more suitable). Unfortunately, when the separator is used in one of the "list" elements, then, the list is broken. An escape character can be chosen, etc. All of this requires conventions and creates an unnecessary maintenance burden.

+ +

Use strings for textual data. When representing complex data, parse strings and use the appropriate abstraction.

+ +

Symbol type

+ +

Symbols are new to JavaScript in ECMAScript 2015. A Symbol is a unique and immutable primitive value and may be used as the key of an Object property (see below). In some programming languages, Symbols are called "atoms".

+ +

For more details see Symbol and the {{jsxref("Symbol")}} object wrapper in JavaScript.

+ +

Oggetti

+ +

In computer science, an object is a value in memory which is possibly referenced by an identifier.

+ +

Proprietà

+ +

In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values. A key value is either a String or a Symbol value.

+ +

There are two types of object properties which have certain attributes: The data property and the accessor property.

+ +

Data property

+ +

Associates a key with a value, and has the following attributes:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Attributes of a data property
AttributoTipoDescrizioneValore di default
[[Value]]Any JavaScript typeThe value retrieved by a get access of the property.undefined
[[Writable]]BooleanIf false, the property's [[Value]] cannot be changed.false
[[Enumerable]]Boolean +

If true, the property will be enumerated in for...in loops.
+ See also Enumerability and ownership of properties.

+
false
[[Configurable]]BooleanIf false, the property cannot be deleted, cannot be changed to an accessor property, and attributes other than [[Value]] and [[Writable]] cannot be changed.false
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
Obsolete attributes (as of ECMAScript 3, renamed in ECMAScript 5)
AttributoTipoDescrizione
Read-onlyBooleanReversed state of the ES5 [[Writable]] attribute.
DontEnumBooleanReversed state of the ES5 [[Enumerable]] attribute.
DontDeleteBooleanReversed state of the ES5 [[Configurable]] attribute.
+ +

Accessor property

+ +

Associates a key with one of two accessor functions (get and set) to retrieve or store a value, and has the following attributes:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Attributes of an accessor property
AttributeTypeDescriptionDefault value
[[Get]]Function object or undefinedThe function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed.
+ See also get.
undefined
[[Set]]Function object or undefinedThe function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed.
+ See also set.
undefined
[[Enumerable]]BooleanIf true, the property will be enumerated in for...in loops.false
[[Configurable]]BooleanIf false, the property can't be deleted and can't be changed to a data property.false
+ +
+

Note: Attribute is usually used by JavaScript engine, so you can't directly access it (see more about {{jsxref("Object.defineProperty()")}}). That's why the attribute is put in double square brackets instead of single.

+
+ +

"Normal" objects, and functions

+ +

A JavaScript object is a mapping between keys and values. Keys are strings (or {{jsxref("Symbol")}}s), and values can be anything. This makes objects a natural fit for hashmaps.

+ +

Functions are regular objects with the additional capability of being callable.

+ +

Dates

+ +

When representing dates, the best choice is to use the built-in Date utility in JavaScript.

+ +

Indexed collections: Arrays and typed Arrays

+ +

Arrays are regular objects for which there is a particular relationship between integer-key-ed properties and the length property.

+ +

Additionally, arrays inherit from Array.prototype, which provides to them a handful of convenient methods to manipulate arrays. For example, indexOf (searching a value in the array) or push (adding an element to the array), and so on. This makes Arrays a perfect candidate to represent lists or sets.

+ +

Typed Arrays are new to JavaScript with ECMAScript 2015, and present an array-like view of an underlying binary data buffer. The following table helps determine the equivalent C data types:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TypeValue RangeSize in bytesDescriptionWeb IDL typeEquivalent C type
{{jsxref("Int8Array")}}-128 to 12718-bit two's complement signed integerbyteint8_t
{{jsxref("Uint8Array")}}0 to 25518-bit unsigned integeroctetuint8_t
{{jsxref("Uint8ClampedArray")}}0 to 25518-bit unsigned integer (clamped)octetuint8_t
{{jsxref("Int16Array")}}-32768 to 32767216-bit two's complement signed integershortint16_t
{{jsxref("Uint16Array")}}0 to 65535216-bit unsigned integerunsigned shortuint16_t
{{jsxref("Int32Array")}}-2147483648 to 2147483647432-bit two's complement signed integerlongint32_t
{{jsxref("Uint32Array")}}0 to 4294967295432-bit unsigned integerunsigned longuint32_t
{{jsxref("Float32Array")}}1.2×10-38 to 3.4×1038432-bit IEEE floating point number (7 significant digits e.g., 1.1234567)unrestricted floatfloat
{{jsxref("Float64Array")}}5.0×10-324 to 1.8×10308864-bit IEEE floating point number (16 significant digits e.g., 1.123...15)unrestricted doubledouble
{{jsxref("BigInt64Array")}}-263 to 263-1864-bit two's complement signed integerbigintint64_t (signed long long)
{{jsxref("BigUint64Array")}}0 to 264-1864-bit unsigned integerbigintuint64_t (unsigned long long)
+ +

Keyed collections: Maps, Sets, WeakMaps, WeakSets

+ +

These data structures, introduced in ECMAScript Edition 6, take object references as keys. {{jsxref("Set")}} and {{jsxref("WeakSet")}} represent a set of objects, while {{jsxref("Map")}} and {{jsxref("WeakMap")}} associate a value to an object.

+ +

The difference between Maps and WeakMaps is that in the former, object keys can be enumerated over. This allows garbage collection optimizations in the latter case.

+ +

One could implement Maps and Sets in pure ECMAScript 5. However, since objects cannot be compared (in the sense of < "less than", for instance), look-up performance would necessarily be linear. Native implementations of them (including WeakMaps) can have look-up performance that is approximately logarithmic to constant time.

+ +

Usually, to bind data to a DOM node, one could set properties directly on the object, or use data-* attributes. This has the downside that the data is available to any script running in the same context. Maps and WeakMaps make it easy to privately bind data to an object.

+ +

Structured data: JSON

+ +

JSON (JavaScript Object Notation) is a lightweight data-interchange format, derived from JavaScript, but used by many programming languages. JSON builds universal data structures.

+ +

See JSON and {{jsxref("JSON")}} for more details.

+ +

More objects in the standard library

+ +

JavaScript has a standard library of built-in objects.

+ +

Please have a look at the reference to find out about more objects.

+ +

Determinare il tipo utilizzando l'operatore typeof

+ +

L'operatore typeof può aiutarti a trovare il tipo di dato assegnato alla tua variabile.

+ +

Si prega di leggere la pagina di riferimento per maggiori dettagli e casi limite.

+ +

Specifiche

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-ecmascript-data-types-and-values', 'ECMAScript Data Types and Values')}}
+ +

Vedi anche

+ + -- cgit v1.2.3-54-g00ecf