Just JavaScript - Real World Objects |
Written by Ian Ellliot | |||
Monday, 13 August 2018 | |||
Page 2 of 2
Advanced Property CreationAs we have discovered you can create a new property by simply using it. However you cannot create a getter or a setter property in this way as you also have to define the get and set functions. To allow for more complex properties to be added dynamically and to allow for more control over how properties behave there is the defineProperty method of the Object constructor. You can use it to add a property to myObject using:
The optional descriptor is an object of key value pairs that defines the property. You can use any of the following keys:
set to true the property is dynamic and its descriptor may be changed and the property deleted. You can still change the value but any attempt to change other characteristics of the property results in a runtime error.
set to true if the property is to be included in an enumeration of properties using for..in say
The value of the property.
set to true if value can change. If the property isn’t writable then attempts to change it’s value fail but no error is thrown.
a getter function for the property
a setter function for the property If you specify get and set you cannot specify writable or value they are mutually exclusive. So for example:
is equivalent to
i.e. the usual way of creating a property. Notice that the defaults do not give you the usual way of creating a property. You can use defineProperty to control how properties behave but note that it was only defined in ES5.1 and only fully defined in ES2015. It the property already exists then any key value pairs you include are used to modify it. There is also a defineProperties method which allows you to define multiple properties:
where properties is an object which consists of name descriptor pairs. For example:
creates myProperty1 and myProperty2 with the descriptors as given. Object CreationWe already have two ways of creating an object as a literal {} or using the Object constructor. There was a need to find a more flexible way to specify the object that was being created and ES5.1 introduced the object factory method create. Object.create works in much the same way as the constructor itself but you can specify another object to be used as the Prototype. Prototypes are described in Chapter Eight and you will find more on Object.create there. So
returns an object with no prototype – this is the purest object you can create as it has no properties at all. You can also specify an object to create properties. This object consists of property names with values that are property descriptors as introduced in the previous section. For example:
Creates an object with a single property, myProperty with the characteristics as given. You can see that Object.create is a much more precise way of creating an object with just the characteristics you need but note that it isn’t supported by older browsers. If you can assume ES5 or ES2015 then it is the preferred way of creating an object but preferable within a constructor – see later. Object MutabilityFinal version in book. Summary
This is an extract from the book Just JavaScript by Ian Elliot. Buy Now: from your local AmazonJust JavaScript
|
|||
Last Updated ( Wednesday, 15 August 2018 ) |