Object.getOwnPropertyDescriptor & Object.defineProperty Methods.
1) Property Descriptor:
When we create a JavaScript object, whether using object literal syntax or some other means and add some properties to it, each property (key) gets a default property descriptor. A property descriptor is a simple JavaScript object associated with each property of the object that contains information about that property such as its value and other meta-data.
2) Access Property Descriptor:
We need to use a static method provided by the Object. The Object.getOwnPropertyDescriptor method returns the property descriptor of the "prop" which is the property name on the "obj" object.
Object.getOwnPropertyDescriptor(obj, prop);
Own here means return the property descriptor of prop property only if that property belongs to the object obj and not on its prototype chain. If the property prop doesn’t exist on obj, it returns undefined.
The Object.getOwnPropertyDescriptor method returns an object with keys describing the setting and current value of the property. It returns the following:
1. The value property of the property descriptor is the current value of the property
2. writable is whether the user can assign a new value to the property
3. enumerable is whether this property will show up in enumerations like for in loop or for loop or Object.keys etc.
4. The configurable property tells whether the user has permission to change property descriptors such as to change the value of writable (not sure because in vs code even if the configurable is false, we are able to change the value of writable) and enumerable settings.
3)Modify Property Descriptor:
Object.defineProperty is used to create a new property on an object or update existing property with a custom descriptor.
When you define a new property on an object using Object.defineProperty and pass an empty {} descriptor, the default descriptor looks like below.
{
value: undefined,
writable: false,
enumerable: false,
configurable: false
}
Homework:
You can create or update multiple properties at once using Object.defineProperties which takes two arguments. First argument is target object on which properties have to be added/modified and second argument is object with key as property name and value as its property descriptor. This function returns the target object.
Basically the syntax is
Object.defineProperty(obj, prop, descriptor)
Where,
obj - The object on which to define the property.
prop -A string or Symbol specifying the key of the property to be defined or modified.
descriptor -
The descriptor for the property being defined or modified.
Using Object.getOwnPropertyDescriptor
Using Object.defineProperty() to modify writable in the property descriptor of propOne property.
Using Object.defineProperty() to add a new property propFour in myObj.
Using Object.defineProperty to add a new property propFive and give it an empty {} property descriptor and also using Object.defineProperty to modify property descriptor of propTwo as empty{}
Using Object.defineProperty() to modify the enumerable of propTwo as false and then using Object.keys(myObj) to count the number of keys/properties left with myObj object.
Using Object.defineProperty() to add new propSix and making its' configurable and writable property descriptors as false and then consoling the result.
5)Multilevel Inheritance
Multi-level inheritance can be defined as an inheritance in which a child class inherits the properties from another child class, which inherited from the parent class and so on.
The multi-level inheritance has multiple base classes.
Class A
|
Class B
|
Class C
JavaScript has only primitive types, null, undefined, and objects.
In JavaScript, there are six primitive data types:
Boolean: A boolean value represents a logical entity and can have two values, either true or false.
Null: The value null represents the intentional absence of any object value.
Undefined: The value undefined is assigned to a variable that is not assigned a value.
Number: A number data type represents numeric values. It can be any integer or floating-point value.
String: A string is a sequence of characters that represent text. It is enclosed in single or double quotes.
Symbol: A symbol is a unique and immutable value that may be used as the key of an Object property.
In JavaScript:
Object is a non-primitive data type that represents a collection of key-value pairs. It can contain properties and methods that can be accessed using dot notation or square bracket notation. Objects can be created using object literals, constructor functions, or the Object.create() method.
null is a primitive value that represents the intentional absence of any object value. It is often used to signify a missing or non-existent value. When a variable is set to null, it means that it has no value or it does not refer to any object.
undefined is a primitive value that represents an uninitialised, non-existent, or undefined value. When a variable is declared but not assigned a value, it is automatically assigned the value of undefined.
Note that null and undefined are distinct values in JavaScript. null is an explicitly assigned value that represents the absence of any object value, while undefined is a default value that is assigned to a variable that is not initialised or does not have a value.








Comments
Post a Comment