Objects, Prototypes and Classes Flashcards
What is an Object literal?
A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. Object literals encapsulate data, enclosing it in a tidy package. This minimizes the use of global variables which can cause problems when combining code.
How do equality operators work with objects?
equality operators compare memory addresses, not object values when using any of the equality operators so comparing two objects with == , ===, or Object.is() equality operators will equal false.
How do you copy or merge the properties of one object into another object?
- Object.assign()
- This takes multiple properties.
- The first is the object that is going to take in new properties.
- The second is the object being copied.
How do you avoid object mutation when using Object.assign()?
Object.assign take in an unlimited number of parameters. The first parameter will always be what gets added to. Therefore, you can make the first parameter in Object.assign() an empty object.
=== and Object.is() provide identical results except when?
- Comparing NaN
- Comparing negative 0 (-0)
Why use bracket notation for objects?
If property names are not valid identifiers (i.e ‘hair color’)
What is the enumerable property on an object?
- Enumerable is an object property that can be included in and visited during a for…in loop or a similar iteration of properties like Object.keys().
- If this property is set to false then the object property can not be iterated over and would not appear in a list of Object.keys().
- Setting enumerable to false also affects JSON serialization of an object.
How can you prevent an objects property from being deleted from an object?
Set the objects configurable property to false. Important - if you change this property to false it is permanent. You cannot change it back to true.
How do you create getters and setters for Objects?
Object.defineProperty(objectName, ‘propertyName’{ get: function(){ return propertyName}, set: function(value) { //code to set property })
What is a prototype?
An object that exists on every function in JavaScript. There are two differnt protypes in JavaScript
- A Functions’s prototype
- An Object’s prototype
Define Functions Prototype
The object instance that will become the prototype for ll objects created using this function as a constructor.
Define Objects Prototype
The object instance from which the object is inherited