Function
defineProperty (obj, keyName, desc, data) public
Module:
@ember/object
Defined in packages/@ember/-internals/metal/lib/properties.ts:13
import { defineProperty } from '@ember/object'; |
- obj
- Object
- the object to define this property on. This may be a prototype.
- keyName
- String
- the name of the property
- desc
- Descriptor
- an instance of `Descriptor` (typically a computed property) or an ES5 descriptor. You must provide this or `data` but not both.
- data
- *
- something other than a descriptor, that will become the explicit value of this property.
NOTE: This is a low-level method used by other parts of the API. You almost
never want to call this method directly. Instead you should use
mixin()
to define new properties.
Defines a property on an object. This method works much like the ES5
Object.defineProperty()
method except that it can also accept computed
properties and other special descriptors.
Normally this method takes only three parameters. However if you pass an
instance of Descriptor
as the third param then you can pass an
optional value as the fourth parameter. This is often more efficient than
creating new descriptor hashes for each property.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import { defineProperty, computed } from '@ember/object'; // ES5 compatible mode defineProperty(contact, 'firstName', { writable: true, configurable: false, enumerable: true, value: 'Charles' }); // define a simple property defineProperty(contact, 'lastName', undefined, 'Jolley'); // define a computed property defineProperty(contact, 'fullName', computed('firstName', 'lastName', function() { return this.firstName+' '+this.lastName; })); |