C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Object.defineProperty() MethodThe Object.defineProperty() method defines a new property directly on an object and returns the object. To change the flags, we can use Object.defineProperty. We cannot change it back, because define property doesn?t work on non-configurable properties. Syntax:Object.defineProperty(obj, prop, descriptor) ParameterObj: The Object on which to define the property. Prop: The name of a property to be defined or modified. Descriptor: The descriptor for the property being defined or modified. Return:This method returns the object that was passed to the function. Browser Support:
Example 1const object1 = {}; Object.defineProperty(object1, 'property1', { value: 22, } ); object1.property1; // throws an error in strict mode console.log(object1.property1); Output: 22 Example 2const object1 = {}; Object.defineProperty(object1, 'property1', { value: 42, value: 52, value: 542, }); object1.property1 = 177; // throws an error in strict mode console.log(object1.property1); Output: 542 Example 3const object1 = {}; Object.defineProperty(object1, 'property1', { value: 2, value: 4, value: 4+13, }); object1.property1 ; console.log(object1.property1); Output: 17
Next TopicJavaScript Objects
|