README.md (4114B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Define Properties 22 23 > [Define][mdn-define-properties] (and/or modify) object properties. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var defineProperties = require( '@stdlib/utils/define-properties' ); 31 ``` 32 33 #### defineProperties( obj, properties ) 34 35 [Defines][mdn-define-properties] (and/or modifies) object properties. 36 37 ```javascript 38 var objectKeys = require( '@stdlib/utils/keys' ); 39 40 var obj = {}; 41 defineProperties( obj, { 42 'foo': { 43 'value': 'bar', 44 'writable': false, 45 'configurable': false, 46 'enumerable': true 47 }, 48 'baz': { 49 'value': 13, 50 'enumerable': false 51 } 52 }); 53 54 // Retrieve all enumerable keys: 55 var keys = objectKeys( obj ); 56 // returns [...] 57 ``` 58 59 The `properties` parameter is an `object` whose own enumerable property values are descriptors for the properties to be defined or modified. A property descriptor has the following optional properties: 60 61 - **configurable**: `boolean` indicating if property descriptor can be changed and if the property can be deleted from the provided object. Default: `false`. 62 - **enumerable**: `boolean` indicating if the property shows up when enumerating object properties. Default: `false`. 63 - **writable**: `boolean` indicating if the value associated with the property can be changed with an assignment operator. Default: `false`. 64 - **value**: property value. 65 - **get**: `function` which serves as a getter for the property, or, if no getter, `undefined`. When the property is accessed, a getter function is called without arguments and with the `this` context set to the object through which the property is accessed (which may not be the object on which the property is defined due to inheritance). The return value will be used as the property value. Default: `undefined`. 66 - **set**: `function` which serves as a setter for the property, or, if no setter, `undefined`. When assigning a property value, a setter function is called with one argument (the value being assigned to the property) and with the `this` context set to the object through which the property is assigned. Default: `undefined`. 67 68 </section> 69 70 <!-- /.usage --> 71 72 <section class="notes"> 73 74 ## Notes 75 76 - Property descriptors come in two flavors: **data descriptors** and **accessor descriptors**. A data descriptor is a property that has a value, which may or may not be writable. An accessor descriptor is a property described by a getter-setter function pair. A descriptor must be **one** of these two flavors and **cannot** be both. 77 78 </section> 79 80 <!-- /.notes --> 81 82 <section class="examples"> 83 84 ## Examples 85 86 <!-- eslint no-undef: "error", no-restricted-syntax: "off" --> 87 88 ```javascript 89 var defineProperties = require( '@stdlib/utils/define-properties' ); 90 91 function Person( name ) { 92 if ( !(this instanceof Person) ) { 93 return new Person( name ); 94 } 95 defineProperties( this, { 96 'name': { 97 'value': name, 98 'writable': false 99 }, 100 'greeting': { 101 'get': function greet() { 102 return 'Hello '+this.name; 103 } 104 } 105 }); 106 return this; 107 } 108 109 var person = new Person( 'Simon' ); 110 111 try { 112 person.name = 'Peter'; 113 } catch ( err ) { 114 console.log( err.message ); 115 } 116 117 var greeting = person.greeting; 118 // returns <string> 119 ``` 120 121 </section> 122 123 <!-- /.examples --> 124 125 <section class="links"> 126 127 [mdn-define-properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties 128 129 </section> 130 131 <!-- /.links -->