Classes and Objects Flashcards
When to use prototypes?
For instance, when creating a new object/class, methods should normally be associated to the object’s prototype rather than defined into the object constructor. The reason is that whenever the constructor is called, the methods would get reassigned (that is, for every object creation).
How to delete a property in an object?
delete object.propertyName
What is computed properties?
We can use square brackets in an object literal. That’s calledcomputed properties. let fruit = prompt("Which fruit to buy?", "apple"); let bag = { [fruit]: 5, // the name of the property is taken from the variable fruit }; alert( bag.apple ); // 5 if fruit="apple"
Can we use reserved word for object properties?
yes. let obj = { for: 1, let: 2, return: 3 };
How would we know if a property exists in the object?
two ways: 1) checking if the value is undefined:
obj. prop === undefined
2) using in => prop in object // would return true or false
How can you loop through the properties of the object?
We can use a for in loop. let user = { name: "John", age: 30, isAdmin: true };
for (let key in user) { // keys alert( key ); // name, age, isAdmin // values for the keys alert( user[key] ); // John, 30, true }
How are properties ordered in an object?
If the properties are numbers, they are sorted ascending and if not numbers, they are displayed in the order of creation.
When we assign an object to another variable, are the whole object added or the reference?
The reference to the object is assigned to the new variable. So now both variables point to the same object and if any of the variables change any properties, it would be seen in the other variable.
Does the equality operator == vary from the strict equality operator === when comparing objects?
When both variables are pointing to the same object the equality and strict equality operator act the same. But if there are 2 different objects, then the equality and strict equality differs.
If we create a const object, can we add properties to it?
Yes, since the variable is only keeping the reference to the object and we can add properties to the object. If we wanted to assign it to another object, then it would give an error. const user = { name: 'Pete'} user = { name: 'John'} => error
How can we clone an object?
1) we can loop through the object keys and assign the same values to the same keys in the second object
2) we can use the Object.assign(dest, object1,object2, …, objectN); let user2 = Object.assign({}, user1);
How is garbage collected in JS?
It is being done automatically. and the main reason for it is Reachability.
What is the basic garbage collector name and how does it work?
The basic garbage collection algorithm is called “mark-and-sweep”.
The following “garbage collection” steps are regularly performed:
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 types can the property name be in objects?
Property names are either strings or symbols.
What is a symbol?
A “symbol” represents a unique identifier.
A value of this type can be created using Symbol():
// id is a new symbol let id = Symbol();
Do Symbols automatically convert to strings?
No, we either have to call toString() on it or we use the description:
let id = Symbol(‘id’);
id.toString() or id.description
What’s the benefit of usingSymbol(“id”)over a string”id” as object properties?
Asuserobjects belongs to another code, and that code also works with them, we shouldn’t just add any fields to it. That’s unsafe. But a symbol cannot be accessed accidentally, the third-party code probably won’t even see it, so it’s probably all right to do.
Also, imagine that another script wants to have its own identifier insideuser, for its own purposes. That may be another JavaScript library, so that the scripts are completely unaware of each other.
Then that script can create its ownSymbol(“id”), like this:
// …
let id = Symbol(“id”);
user[id] = “Their id value”;
There will be no conflict between our and their identifiers, because symbols are always different, even if they have the same name.
…But if we used a string”id”instead of a symbol for the same purpose, then therewouldbe a conflict:
let user = { name: “John” };
// Our script uses “id” property
user.id = “Our id value”;
// …Another script also wants “id” for its purposes…
user.id = “Their id value”
// Boom! overwritten by another script!
How can we add symbols to objects?
Once we create a symbol: let id = Symbol('id'); we can then add it to the object by placing it in square brackets: let user = { name: 'Joe', [id]: 123};