JavaScript Objects Flashcards
How do you create a JS object?
let user = new Object(); // "object constructor" syntax let user = {}; // "object literal" syntax
How do you remove an attribute from an object?
With “delete”
delete user.age;
Can you use mutliword property names for objects?
Yes, but they must be quoted:
let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };
How do you access multiword properties?
With square notation:
user[“likes birds”]
What is the result of the following code?
let fruit = ‘Apple’
let bag = { [fruit]: 5, };
alert( bag.apple );
5
What is the value of “bag”?
let fruit = 'apple'; let bag = { [fruit + 'Computers']: 5 };
{
appleComputers: 5
}
What is printed?
let obj = { for: 1, let: 2, return: 3 };
alert( obj.for + obj.let + obj.return );
6
What values can be used as object property names?
Basically, any name is allowed, but there’s a special one: “__proto__” that gets special treatment for historical reasons.
What is the result of the following?
let name=0; let age = 0; return { name, age };
{
name: 0,
age: 0
}
How can you verify if a property exist in an object?
With the “in” operator.
“key” in obj // true if it exists
What will the second alert print?
let obj = { test: undefined }; alert( obj.test ); // it's undefined, so - no such property? alert( "test" in obj );
true
The property obj.test technically exists. So the in operator works right.
How do you iterate through all the keys in an object using a “for” statement
With “for .. in”
Are the properties in an object ordered when accessing them with a “for .. in” loop?
Yes, they are “ordered in a special fashion”: integer properties are sorted, the all others appear in creation order.
What will the alert print?
let user = { name: 'John' }; let admin = user; admin.name = 'Pete'; alert(user.name);
‘Pete’,
the property was updated by the “admin” reference but changes are seen from the “user” reference and both point to the same object
What will the two alerts print?
let a = {}; let b = a; // copy the reference
alert( a == b );
alert( a === b );
true and true
both variables reference the same object
When comparing two objects, what is the difference between == and ===?
Nothing, both work exactly the same
What will happen with the following constant-declared object?
const user = { name: "John" };
user.age = 25;
alert(user.age);
The alert will print 25.
Constant-defined objects can change their property values. What cannot change is the value of “user” itself
What will happen with the following constant-declared object?
const user = { name: "John" };
// Error (can't reassign user) user = { name: "Pete" };
An error will be thrown. Can’t reassign constant variables
How can you create the copy of an object?
By copying each property, one by one, to a new object, or by using Object.assign().
What does “deep clone” an object mean?
Copying an object, making sure that if one or many properties are objects themselves, those objects are also cloned
What does “Object.assign”
It takes multiple objects as parameters and returns a new object which contains all the cloned properties of the parameters
How is memory managed (allocated and freed) in JavaScript
Memory is allocated in the heap and it’s freed by the Garbage Collector
When is memory freed in JavaScript?
When objects are not reachable, they are marked by the Garbage Collector for deletion.
When is an object considered “unreachable”?
When there are no variables with references pointing tot he object
What is an internal algorithm used by the Garbage Collector in JavaScript?
It’s mark-and-sweep
How does “mark-and-sweep” word?
The garbage collector takes roots and “marks” (remembers) them.
Then it visits and “marks” all references from them.
Then it visits marked objects and marks their references. All visited objects are remembered, so as not to visit the same object twice in the future.
…And so on until every reachable (from the roots) references are visited.
All objects except marked ones are removed.
What are some optimizations done for the Garbage Collector to not affect execution?
Generational collection, incremental collection and idle-time collection
What is “general collection” in the Garbage Collector?
Objects are split into two sets: “new ones” and “old ones”. Many objects appear, do their job and die fast, they can be cleaned up aggressively. Those that survive for long enough, become “old” and are examined less often.