Objects Flashcards
What is the difference between Object.assign(target,source) and { ..obj1 , obj2} ?
In object.assign() the target object is modified.
But using a spread operator will not modify the existing objects
const target ={a:1,b:2} const source = {a:10, b:40}
const obj = Object.assign(target , source) console.log(obj) // prints { a: 10, b: 40 }
console.log(target) // prints { a: 10, b: 40 } . It has modified the target obj
What is the difference between object and map
- Objects can have only strings and symbols as their keys while Map can have primitive, functions or even objects as their keys.
- Keys in Map are ordered but not in objects.
- Size of Map can be easily determined using size property while it’s relatively hard for objects.
- A Map may perform better in scenarios involving frequent addition and removal of key pairs.
How to create copy of a given object ?
Use object.assign()
const obj = { a: 1 }; const copy = Object.assign({}, obj); console.log(copy); // { a: 1 }
How to access entries in Object and Array
Object.entries({city:”hyd”, country :”india”})
Need to pass the obj in Object.entries() function
[1,2,3,4].entires()
In array, just call the entries() function on a give array
What is the difference between Object.freeze() and Object.seal()?
Objects sealed with Object.seal() can have their existing properties changed. Existing properties in objects frozen with Object.freeze() are made immutable.
- The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable
example: Object.freeze(); const orgObj = { 1: 2, 2: 3 }; const obj = Object.freeze(orgObj);
obj === orgObj; // prints true
obj == orgObj; // prints true
- The Object.freeze() method freezes an object.
No adding or removing properties, no modifying existing properties, prevents changing the enumerability, configurability, or writability of existing properties,
prevents its prototype from being changed.
freeze() returns the same object that was passed in.
const obj = { city: "hyd", country: "india" }; const sealedObj = Object.seal(obj);
sealedObj == obj; //true
sealedObj === obj; //true
sealedObj.city = “pune”; //{ city: ‘pune’, country: ‘india’ }
delete sealedObj.city; //false
console.log(sealedObj); //after deleting city, obj remains unmodified as { city: ‘pune’, country: ‘india’ }
What happens if we create a object using new keyword with undefined and null?
let o = new Object(undefined) let o = new Object(null)
Both return { }
empty object
delete operator
The delete operator removes a given property from an object. On successful deletion, it will return true, else false will be returned.
When trying to delete a property that does
not exist, true is returned
// delete does not affect built-in static properties. delete Math.PI; // returns false
var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple']; delete trees[3];
When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.