time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (3514B)


      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 Property
     22 
     23 > [Define][mdn-define-property] (or modify) an object property.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var defineProperty = require( '@stdlib/utils/define-property' );
     31 ```
     32 
     33 #### defineProperty( obj, prop, descriptor )
     34 
     35 [Defines][mdn-define-property] (or modifies) an object property.
     36 
     37 ```javascript
     38 var obj = {};
     39 
     40 defineProperty( obj, 'foo', {
     41     'value': 'bar',
     42     'writable': false
     43 });
     44 
     45 obj.foo = 'boop';
     46 // throws <Error>
     47 ```
     48 
     49 A property `descriptor` has the following optional properties:
     50 
     51 -   **configurable**: `boolean` indicating if property descriptor can be changed and if the property can be deleted from the provided object. Default: `false`.
     52 -   **enumerable**: `boolean` indicating if the property shows up when enumerating object properties. Default: `false`.
     53 -   **writable**: `boolean` indicating if the value associated with the property can be changed with an assignment operator. Default: `false`.
     54 -   **value**: property value.
     55 -   **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`.
     56 -   **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`.
     57 
     58 </section>
     59 
     60 <!-- /.usage -->
     61 
     62 <section class="notes">
     63 
     64 ## Notes
     65 
     66 -   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.
     67 
     68 </section>
     69 
     70 <!-- /.notes -->
     71 
     72 <section class="examples">
     73 
     74 ## Examples
     75 
     76 <!-- eslint no-undef: "error" -->
     77 
     78 ```javascript
     79 var defineProperty = require( '@stdlib/utils/define-property' );
     80 
     81 function Foo( name ) {
     82     if ( !(this instanceof Foo) ) {
     83         return new Foo( name );
     84     }
     85     defineProperty( this, 'name', {
     86         'value': name,
     87         'configurable': true
     88     });
     89     return this;
     90 }
     91 
     92 var foo = new Foo( 'beep' );
     93 
     94 try {
     95     delete foo.name;
     96     console.log( '`name` property successfully deleted.' );
     97 } catch ( err ) {
     98     console.error( err.message );
     99 }
    100 ```
    101 
    102 </section>
    103 
    104 <!-- /.examples -->
    105 
    106 <section class="links">
    107 
    108 [mdn-define-property]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
    109 
    110 </section>
    111 
    112 <!-- /.links -->