Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Professional Javascrip for Webdevelopers - page 177
- // working example using object literal declaration and defineProperty()
- //
- // i just changed a logic a bit, so when the new version of the book
- // is released in 2007, it becomes 2nd edition not 4th (1 + 2007 - 2004)
- // as it is in the example in the book
- var book = {};
- book = {
- _year: 2004,
- edition: 1
- };
- Object.defineProperty(book, "year",{
- get: function(){
- return this._year;
- },
- set: function(newValue){
- /* original code
- if(newValue > 2004){
- this._year = newValue;
- this.edition += newValue - 2004;
- }
- */
- if(newValue > this._year){
- this._year = newValue;
- this.edition++;
- }
- }
- });
- book.year = 2007;
- console.log(book.edition);
- //2
- Object.getOwnPropertyDescriptor(book, "_year");
- //Object {value: 2007, writable: true, enumerable: true, configurable: true}
- Object.getOwnPropertyDescriptor(book,"edition");
- //Object {value: 2, writable: true, enumerable: true, configurable: true}
- Object.getOwnPropertyDescriptor(book, "year");
- //Object {get: function, set: function, enumerable: false, configurable: false}
- /*****************************************/
- // not working and NOT identical to the example above
- /* In the first example _year can be set to different value, because it was created
- using object literal notation, so its writable, enumerable and configurable attributes
- are set to true which means that it can be changed by the year get method.
- In the defineProperties example below, these attributes are set to false by default,
- so the _year and edition can't be changed and the code isn't working as expected.
- */
- var book = {};
- Object.defineProperties(book,{
- _year: {
- value: 2004
- },
- edition: {
- value: 1
- },
- year: {
- get: function(){
- return this._year;
- },
- set: function(newValue){
- if(newValue>this._year){
- this._year = newValue;
- this.edition++;
- }
- }
- }
- });
- book.year = 2007;
- console.log(book.year); //2004
- console.log(book.edition); //1
- Object.getOwnPropertyDescriptor(book,"_year");
- //Object {value: 2004, writable: false, enumerable: false, configurable: false}
- Object.getOwnPropertyDescriptor(book,"edition");
- //Object {value: 1, writable: false, enumerable: false, configurable: false}
- Object.getOwnPropertyDescriptor(book,"year");
- //Object {get: function, set: function, enumerable: false, configurable: false}
- /*****************************************/
- // working and identical to the first example
- var book = {};
- Object.defineProperties(book,{
- _year: {
- value: 2004,
- writable: true,
- enumerable: true,
- configurable: true
- },
- edition: {
- value: 1,
- writable: true,
- enumerable: true,
- configurable: true
- },
- year: {
- get: function(){
- return this._year;
- },
- set: function(newValue){
- if(newValue>this._year){
- this._year = newValue;
- this.edition++;
- }
- }
- }
- });
- book.year = 2007;
- console.log(book.year); // 2007
- console.log(book.edition); // 2
- book.year = 2009;
- console.log(book.edition); // 3
Advertisement
Add Comment
Please, Sign In to add comment