Objects Flashcards

(140 cards)

1
Q

What is an object ?

A

An object is a composite value: it aggregates multiple values (primitive values or other objects) and allows you to store and retrieve those values by name.

An object is an unordered collection of properties, each of which has a name and a value. Property names are usually strings (although, as we’ll see in §6.10.3, property names can also be Symbols), so we can say that objects map strings to values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the other common names of “string-to-value” mapping structures in other programming languages ?

A

hash, hashtable, dictionary, associative array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What makes a javascript object something more than a simple “string-to-value” map ?

A

A javascript object also inherits the properties of another object, known as its “prototype”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are types possible for the property names of an object

A

String (most common one) and Symbol.
The other types are converted to String.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the possible values of a javascript object property ?

A

Any javascript value, or a getter or setter function (or both)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a prototype ?

A

A prototype is an object that make one or many other objects inherits of its properties.

Almost every javascript object has a second javascript object associated with it: its prototype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What property of the constructor function is used as the prototype for objects created with the new keyword?

A

The value of the prototype property of the constructor function

This means that the newly created object inherits properties and methods from the constructor’s prototype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: Objects created using the new keyword do not inherit from the constructor’s prototype.

A

False

Objects created with the new keyword inherit from the prototype of the constructor function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens to the prototype of an object created using a constructor function?

A

It uses the value of the prototype property of the constructor function

This establishes a prototype chain for the newly created object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is unique about Object.prototype?

A

It has no prototype and does not inherit any properties.

This makes Object.prototype a rare object in JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Do most built-in constructors inherit from Object.prototype?

A

Yes, most built-in constructors and user-defined constructors have a prototype that inherits from Object.prototype.

Examples include constructors like Date, Array, and others.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the term for the linked series of prototype objects?

A

Prototype chain.

This chain allows objects to inherit properties from one another in JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False: Object.prototype inherits properties from another object.

A

False.

Object.prototype is the base object and does not have a prototype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Fill in the blank: Most user-defined constructors have a prototype that inherits from __________.

A

Object.prototype.

This is a fundamental aspect of JavaScript’s prototypal inheritance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does Object.create() do?

A

Creates a new object, using its first argument as the prototype of that object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What properties does o1 inherit in the example let o1 = Object.create({x: 1, y: 2})?

A

x and y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What happens when you pass null to Object.create()?

A

Creates a new object that does not have a prototype, inheriting no properties or methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the effect of creating an object with let o2 = Object.create(null)?

A

o2 inherits no properties or methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you create an ordinary empty object using Object.create()?

A

Pass Object.prototype as the argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the example code for creating an ordinary empty object with Object.create()?

A

let o3 = Object.create(Object.prototype)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does the second argument of Object.create() do?

A

Describes the properties of the new object (advanced feature)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is one use case for Object.create() mentioned in the text?

A

Guard against unintended modifications of an object by a library function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the purpose of passing Object.create(o) to a library function?

A

To prevent accidental modifications to the original object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What will happen if the library function sets properties on the object created by Object.create(o)?

A

Those writes will not affect the original object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
True or False: If you create an object with Object.create(null), it can still use basic methods like toString().
False
26
What must the expression inside the square brackets evaluate to?
A string or a value that can be converted to a string or to a Symbol ## Footnote This is specified in §6.10.3.
27
Fill in the blank: The expression inside the square brackets must evaluate to a _______.
string or a value that can be converted to a string or to a Symbol
28
True or False: The square bracket notation is only applicable to strings.
False ## Footnote It can also apply to values that can be converted to a string or to a Symbol.
29
How do JavaScript objects inherit properties?
From their prototype object through the prototype chain
30
When querying a property, what happens if the object does not have that property?
The prototype object is queried for the property
31
What is the prototype chain?
A linked list of objects from which properties are inherited
32
What happens when you assign a value to an existing own property?
The value of the existing property is changed
33
What occurs when you assign a value to a property that does not exist on the object?
A new property is created on the object
34
What is hidden when a new own property is created with the same name as an inherited property?
The inherited property is hidden
35
How does property assignment work in relation to the prototype chain?
It examines the prototype chain only to determine if the assignment is allowed
36
What happens if a property is inherited and is read-only?
The assignment is not allowed
37
What is the effect of property assignment on the prototype chain?
It never modifies objects in the prototype chain
38
What is a key feature of JavaScript regarding property inheritance?
Inheritance occurs when querying properties but not when setting them
39
What happens when an object overrides an inherited property?
The inherited property remains unchanged in the prototype
40
What is an accessor property in JavaScript?
A property that has a getter and/or setter method
41
True or False: Property assignments in JavaScript modify the prototype chain.
False
42
What happens when you delete a property that does not exist?
The delete expression evaluates to true.
43
Can the delete operator remove inherited properties?
No, it only deletes own properties.
44
How can you delete an inherited property?
Delete it from the prototype object.
45
What is the result of deleting a non-configurable property in strict mode?
Causes a TypeError.
46
What will the delete operator return when used on a non-configurable property in non-strict mode?
Evaluates to false.
47
What is the result of the expression 'delete Object.prototype'?
Evaluates to false: property is non-configurable.
48
Can you delete a global variable created by variable declaration?
No, it cannot be deleted.
49
What happens in strict mode if you try to delete an unqualified identifier like x?
Raises a SyntaxError.
50
Fill in the blank: The delete operator only deletes ______ properties.
own
51
True or False: The delete operator can remove properties with a configurable attribute of true.
True.
52
What happens when you delete a property that has already been deleted?
Evaluates to true.
53
What is the purpose of enumerating properties in JavaScript?
To iterate through or obtain a list of all the properties of an object.
54
Which loop is commonly used to enumerate properties of an object?
for/in loop
55
What does the for/in loop assign to the loop variable?
The name of the property
56
Are built-in methods that objects inherit enumerable?
No, they are not enumerable.
57
Give an example of an object with enumerable properties.
let o = {x: 1, y: 2, z: 3};
58
What will o.propertyIsEnumerable('toString') return?
false
59
How can you skip inherited properties in a for/in loop?
By checking with o.hasOwnProperty(p)
60
What can you use to get an array of property names from an object?
Object.keys(), Object.getOwnPropertyNames(), Object.getOwnPropertySymbols(), Reflect.ownKeys()
61
What does Object.keys() return?
An array of the names of the enumerable own properties of an object.
62
What does Object.getOwnPropertyNames() return?
An array of the names of non-enumerable own properties as well.
63
What does Object.getOwnPropertySymbols() return?
Own properties whose names are Symbols.
64
What does Reflect.ownKeys() return?
All own property names, both enumerable and non-enumerable, and both string and Symbol.
65
What is the order of property enumeration defined by ES6?
String properties named with non-negative integers first, then remaining string properties, and finally Symbol properties.
66
In what order are string properties with non-negative integers listed?
In numeric order from smallest to largest.
67
How are properties that look like array indexes enumerated?
They are listed first in numeric order.
68
How are remaining string properties listed?
In the order they were added to the object.
69
What is the enumeration order for Symbol properties?
In the order in which they were added to the object.
70
Is the enumeration order for the for/in loop tightly specified?
No, it is not as tightly specified.
71
What happens if a property by the same name has already been enumerated?
It will not be enumerated again.
72
What is the ES6 method used for copying properties from one object to another?
Object.assign()
73
What does Object.assign() modify and return?
The first argument, which is the target object
74
Does Object.assign() alter the source objects?
No, it does not alter the source objects
75
What types of properties does Object.assign() copy to the target object?
Enumerable own properties of the source object, including Symbol properties
76
How does Object.assign() handle properties with the same name?
Properties in the first source object override properties in the target object
77
What happens if a source object has a getter method during the copy process?
The getter method is invoked, but it is not copied
78
What is one reason to copy properties from one object to another?
To copy default values into another object if a property does not already exist
79
What is the potential issue with using Object.assign() for copying defaults?
It overwrites everything in the target object with defaults
80
How can you avoid overwriting existing properties when copying defaults?
Create a new object and copy defaults into it, then override with properties in the target object
81
How can you express the object copy-and-override operation using the spread operator?
o = {...defaults, ...o}
82
What does the merge() function do differently than Object.assign()?
It only copies properties if they are missing in the target object
83
What is an example of using Object.assign() with properties?
Object.assign({x: 1}, {x: 2, y: 2}, {y: 3, z: 4}) // => {x: 2, y: 3, z: 4}
84
What could a subtract() function do?
Remove all of the properties of one object from another object
85
What is object serialization?
The process of converting an object’s state to a string from which it can later be restored.
86
Which functions are used to serialize and restore JavaScript objects?
JSON.stringify() and JSON.parse()
87
What does JSON stand for?
JavaScript Object Notation
88
How is JSON syntax related to JavaScript syntax?
JSON syntax is a subset of JavaScript syntax.
89
What types of values can be serialized and restored using JSON?
* Objects * Arrays * Strings * Finite numbers * true * false * null
90
What happens to NaN, Infinity, and -Infinity during serialization?
They are serialized to null.
91
How are Date objects serialized in JSON?
They are serialized to ISO-formatted date strings.
92
What happens to Date objects when using JSON.parse()?
They remain in string form and do not restore the original Date object.
93
Can Function, RegExp, and Error objects be serialized or restored?
No, they cannot be serialized or restored.
94
What happens to the undefined value during serialization?
It cannot be serialized or restored.
95
What does JSON.stringify() serialize?
Only the enumerable own properties of an object.
96
What happens if a property value cannot be serialized?
That property is simply omitted from the stringified output.
97
What optional arguments can JSON.stringify() and JSON.parse() accept?
They can accept arguments to customize the serialization and/or restoration process.
98
Fill in the blank: JSON.stringify() and JSON.parse() can convert certain values during the _______.
[serialization or stringification process]
99
True or False: JSON can represent all JavaScript values.
False
100
What is the purpose of the Date.toJSON() function?
To serialize Date objects to ISO-formatted date strings.
101
What type of properties do objects inherit from Object.prototype?
Methods
102
What is the purpose of the toString() method?
Returns a string that represents the value of the object
103
What does the default toString() method return for an object?
[object Object]
104
How can you define a custom toString() method for an object?
By creating a function that returns a string representation of the object
105
What is the purpose of the toLocaleString() method?
Returns a localized string representation of the object
106
What does the default toLocaleString() method do?
Calls toString() and returns that value
107
Which classes define customized versions of toLocaleString()?
Date and Number classes
108
How does Array's toLocaleString() method differ from its toString() method?
Formats array elements by calling their toLocaleString() methods
109
What is the valueOf() method used for?
Converts an object to a primitive type, typically a number
110
When is the valueOf() method called automatically?
When an object is used in a context requiring a primitive value
111
What does the Date class's valueOf() method do?
Converts dates to numbers for chronological comparison
112
What does the toJSON() method do?
Called by JSON.stringify() to serialize an object
113
What happens if a toJSON() method exists on an object during serialization?
It is invoked, and its return value is serialized
114
What does the point object's toJSON() method return in the given example?
The string representation of the point
115
Fill in the blank: The _______ method is used for conversions to numbers.
valueOf()
116
True or False: The toString() method takes arguments.
False
117
What does the expression Number(point) evaluate to if point's valueOf() returns the distance from the origin?
The calculated distance
118
What will point.toLocaleString() return if the point object is defined with localized formatting?
A string representation with localized number formatting
119
What is the ES6 shorthand properties syntax?
Allows creating an object with properties using variable names without repeating them ## Footnote Example: let o = { x, y } instead of let o = { x: x, y: y }.
120
What are computed property names in ES6?
Property names that are defined using expressions enclosed in square brackets ## Footnote Example: let o = { [PROPERTY_NAME]: 1 }.
121
What are Symbols in JavaScript?
Unique and immutable primitive values used as property names ## Footnote Symbols are created using the Symbol() function.
122
What is the spread operator in ES2018?
A syntax that allows copying properties from one object to another using ... ## Footnote Example: let rect = { ...position, ...dimensions }.
123
How do you define a method in an object literal in ES6?
Using shorthand syntax by omitting the function keyword and colon ## Footnote Example: let square = { area() { return this.side * this.side; } }.
124
What are accessor properties in JavaScript?
Properties that use getter and/or setter methods instead of a single value ## Footnote Example: get accessorProp() { return this.dataProp; }.
125
What is the purpose of a getter method?
To return the value of a property when it is accessed ## Footnote Example: get r() { return Math.hypot(this.x, this.y); }.
126
What is the purpose of a setter method?
To set the value of a property when it is assigned ## Footnote Example: set r(newvalue) { this.dataProp = newvalue; }.
127
What happens if both getter and setter methods are defined for a property?
The property is read/write ## Footnote If only a getter is defined, it is read-only; if only a setter is defined, it is write-only.
128
What is the result of using the spread operator with objects that have the same property names?
The last property value will override the previous one ## Footnote Example: let p = { x: 0, ...o }; p.x will take the value from o.
129
True or False: Symbols can be used to create non-conflicting property names.
True ## Footnote Each Symbol is unique, making them ideal for property names.
130
Fill in the blank: Accessor properties can be defined with an extension to the object literal syntax using ______ and ______.
get and set
131
What is the significance of the this keyword in getter and setter methods?
It refers to the object on which the methods are defined ## Footnote Allows access to other properties of the same object.
132
What type of properties can be created using Symbols?
Unique property names that do not conflict with other properties ## Footnote Example: let o = { [symbol]: value }.
133
What does the spread operator do in terms of performance?
It can represent an O(n) operation, impacting performance with large objects ## Footnote Using ... in a loop could lead to inefficient algorithms.
134
What does a read-only accessor property do?
Only has a getter method and cannot be written to ## Footnote Example: get theta() { return Math.atan2(this.y, this.x); }.
135
What is the role of accessor properties in object prototypes?
They can be inherited just like data properties ## Footnote Allows for shared behavior across objects.
136
What is an example of using accessor properties for a serial number generator?
It generates strictly increasing serial numbers using a getter and setter ## Footnote Example: get next() { return this._n++; }.
137
What does the syntax let p = { [PROPERTY_NAME]: 1 } illustrate?
The use of computed property names in object literals ## Footnote Allows dynamic property naming.
138
True or False: The spread operator can spread inherited properties of an object.
False ## Footnote It only spreads the object's own properties.
139
What is the purpose of using Symbols in third-party code?
To add properties without risking name conflicts ## Footnote Ensures properties remain unchanged by third-party code.
140
Fill in the blank: In ES6, you can define methods using ______ syntax.
shorthand