In order to define a property we use Object’s defineProperty method on a particular object. There is the value we want to initialize with, which is the passed in parameter of var firstName. The second writable simply means if we can write over it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var createPerson = function(firstName, lastName) { var person = {}; Object.defineProperties(person, { firstName : { value: firstName, writable : true //configurable, can we reconfigure it with another definition //enumerable: let others enumerate over this object using this property }, lastName : { value: lastName, writeable : true }, fullName : { get : function() { return this.firstName + ", " + this.lastName; }, //getter set : function(onlyOneValue) { } //A setter is invoked with the one value that is assigned to the property (obj.joe = "doe";) - you cannot assign multiple values at once. } }); Object.defineProperty(person, "firstName", { value: firstName, writable : true }); }; var person = createPerson("John", "Doe"); |