JS Variables, Types and Collections (23%)

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is a Map?

A map object holds key value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value A map object iterates it's elements in insertion order - a for...of loop returns an array of [key, value] pair for each iteration

What is an object?

A value in memory that is possibly referenced by an identifier (sequence of characters in the code that identifies a variable, function or property)

Primitive Values: Undefined

A variable that has not yet been assigned is undefined

Object Data Property

Associates a key with a value, and has the following attributes: Attribute: Value, Type: Any JS type, Description: the value retrieved by a get access of the property, Default value: undefined Attribute: Writeable, Type: Boolean, Description: if false, the properties value cannot be changed, Default value: false Attribute: Enumerable, Type: Boolean, Description: if true, the property is I'll be enumerated in for...in loops Attribute: Configurable, Type: Boolean, Description: if false, the property cannot be deleted, cannot be changed to an accessory property and attributes other than value and writeable cannot be changed, Default value: false

Object 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: Attribute: Get, Type: function object or undefined, Description: the function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed, Default value: undefined Attribute: Set, Type: function object or undefined, Description: the function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed, Default value: undefined Attribute: Enumerable, Type: Boolean, Description: if true, the property will be enumerated in for...in loops, default value: false Attribute: configurable, type: Boolean, description: if false, the property can't be deleted and can't be changed to a data property, default value: false

Primitive Values: Boolean

Can have two values: true/false

What is a WeakMap?

Collection of key/value pairs in which the keys are weakly references. The keys must be objects and the values can be arbitrary values. Primitive data types as keys are not allowed Hold weak references to key objects, which means that they do not prevent garbage collection in case there would be no other reference to the key object. Because we he references are weak, WeakMap keys are not Enumerable. There is no method to obtain a list of the keys.

JSON object in JS

Contains methods for parsing JSON and converting values to JSON. It can't be called or constructed Is syntax for serializing objects, arrays, numbers, strings, Boolean and null Objects and arrays: property names must be double quoted strings. Trailing commas are forbidden Numbers: leading zeros are prohibited. A decimal point must be followed by at least one digit. NaN and Infinity are unsupported Any JSON text is a valid JS expression

"Normal" objects and functions

Dates: when representing dates, the best choice is to use the built in date utility in JS 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 such as indexOf or push Typed arrays present an array like view of an underlying binary data buffer Keyed Collections: Maps, Sets, WeakMaps, and WeakSets: difference between map and WeakMap is that in Maps, the object keys can be enumerated over. This allows garbage collection optimization in the latter case Structured data: JSON => lightweight data-interchange format, derived from JS, that builds universal data structures

Primitive Values: Null

Null represents a reference that points to a non existent or invalid object or address. In JS, every object is derived from null value and therefore the typeof operator returns object for it Typeof null === 'object' //true

Primitive Values: Null

Null type has exactly one value: null

Structural root in JS

Null: if object is not inherited, then null is shown

Primitive Values: Number and BigInt

Number is a double-precision 64 bit binary format IEEE 754 value. Has three symbolic values: +Infinity, -Infinity, and NaN (Not a number) BigInt represents numbers with arbitrary precision. Is created by appending n to the end of an integer or by calling the constructor. You are able to use all normal operators. (Cannot he operated on interchangeably with Number — will throw type error)

Structural types in JavaScript

Object: new Object, new Map, new Array, new Set, new WeakMap, new WeakSet, new Date Function: a non data structure, shorthand for function constructor. Though every function constructor is derived from object constructor.

Object Properties

Objects can be seen as a collection of properties Object literal syntax: var Car = { Type: 'Toyota', Make: 'Tundra' } Property values can be of any type, including other objects, and can be added or removed Properties are identified from a key value (similar to a map in apex). The key value is either a string or a symbol value

What's the difference between Maps and Objects?

Similarities: - set keys to values - retrieve those values - delete keys - detect whether something is stored at a key Differences: Accidental Keys: a map does not contain any keys by default, rather only what you explicitly put into it. An object has a prototype which might collide with your own keys Key types: a maps keys can be any value. Objects can only be strings or symbols Key Order: keys in a map are ordered in a simple, straightforward way. Should not rely on object order Size: the number of items in a map is easily retrieved from its size property. Object cannot offer this Iteration: map is an it stable, so it can be directly iterated Performance: performs better in scenarios involving frequent additions and removals of key value pairs To set values in an object, one can use the following structure: Foo['bar'] = 'value' To set values in a map, one can use: Let contacts = new Map() contacts.set('Taylor', {phone: "303-303-3030"})

Primitive Values: Strings

Strings are immutable meaning that once it is created, it is impossible to modify it. It is possible to manipulate copies of the string

What is the typeof operator?

The typeof operator can help you find the type of your variable

Data Types in JavaScript

Undefined, Boolean, number, string, bigint, symbol

What's the difference between undefined and null?

Undefined: something that does not yet exist or does not exist anymore Null: representation of something that exists being empty

Primitive Values: Symbols

Unique and immutable primitive value and may be used as a key of an Object property

What is a WeakSet?

WeakSet objects are collections of objects. Just as Sets, each object in a WeakSet may occur only once; all objects in a WeakSets collection are unique. Use case: Detecting circular references Functions that call themselves recursively need a way of guarding against circular data structures by tracking with objects have already been processed.

What is the difference between a WeakSet and a Set?

WeakSets are collections of objects only. They cannot contain arbitrary values of any type, as Sets can The WeakSet is weak, meaning references to objects in a WeakSet are held weakly. If no other references to an object stored in the WeakSet exist, those objects can be garbage collected

Is JS a loosely typed and dynamic language?

Yes. Variables in JS are not directly associated with any particular value type and any variables can be assigned or reassigned let foo = 42 foo = 'bar'


Conjuntos de estudio relacionados

Subject, Predicate, DO, IO, OP, PN, PA

View Set

Foundations - Parenteral Questions

View Set

Business Principals BMGT 1010 ch.9

View Set

Primerica - UCANPASS - Chapter Taxes, Retirement and Other Insurance Concepts

View Set

CH.46 Reproduction Mastering Biology

View Set

Reccomended Regimens for most people with HIV

View Set

Values and Ethics- True or False

View Set