Object.defineProperty prevents accidental assignment of values ββto a certain key in its prototype chain . With this method, you assign only to this specific level of the object (but not the key in the prototype chain).
For example: There is such an object as {key1: value1, key2: value2} , and you do not know exactly its prototype chain or miss it by mistake, and somewhere in the prototype chain there is some kind of property 'color' then-
using dot (.) assignment-
this operation will assign a value to the 'color' key in the prototype chain (if the key exists somewhere), and you will find the object without changes as. obj.color = 'blue'; // the object remains the same as {key1: value1, key2: value2}
using the Object.defineProperty method -
Object.defineProperty(obj, 'color', { value: 'blue' });
// now the object looks like {key1: value1, key2: value2, color: 'blue'} . he adds the property to the same level. You can then iterate safely using the Object.hasOwnProperty() method.
Anish Choudhary Nov 10 '18 at 12:11 2018-11-10 12:11
source share