Guest User

Untitled

a guest
Jul 7th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Professional Javascrip for Webdevelopers - page 177
  2. // working example using object literal declaration and defineProperty()
  3. //
  4. // i just changed a logic a bit, so when the new version of the book
  5. // is released in 2007, it becomes 2nd edition not 4th (1 + 2007 - 2004)
  6. // as it is in the example in the book
  7.  
  8. var book = {};
  9. book = {
  10.     _year: 2004,
  11.     edition: 1
  12. };
  13.  
  14. Object.defineProperty(book, "year",{
  15.     get: function(){
  16.             return this._year;
  17.         },
  18.     set: function(newValue){
  19.             /* original code
  20.             if(newValue > 2004){
  21.                 this._year = newValue;
  22.                 this.edition += newValue - 2004;
  23.             }
  24.             */
  25.             if(newValue > this._year){
  26.                 this._year = newValue;
  27.                 this.edition++;
  28.             }
  29.         }
  30. });
  31.  
  32. book.year = 2007;
  33.  
  34. console.log(book.edition);
  35. //2
  36.  
  37. Object.getOwnPropertyDescriptor(book, "_year");
  38. //Object {value: 2007, writable: true, enumerable: true, configurable: true}
  39. Object.getOwnPropertyDescriptor(book,"edition");
  40. //Object {value: 2, writable: true, enumerable: true, configurable: true}
  41. Object.getOwnPropertyDescriptor(book, "year");
  42. //Object {get: function, set: function, enumerable: false, configurable: false}
  43.  
  44. /*****************************************/
  45. // not working and NOT identical to the example above
  46. /*  In the first example _year can be set to different value, because it was created
  47.     using object literal notation, so its writable, enumerable and configurable attributes
  48.     are set to true which means that it can be changed by the year get method.
  49.     In the defineProperties example below, these attributes are set to false by default,
  50.     so the _year and edition can't be changed and the code isn't working as expected.
  51. */ 
  52.  
  53.  
  54. var book = {};
  55. Object.defineProperties(book,{
  56.     _year: {
  57.         value: 2004
  58.     },
  59.     edition: {
  60.         value: 1
  61.     },
  62.  
  63.     year: {
  64.         get: function(){
  65.                 return this._year;
  66.             },
  67.         set: function(newValue){
  68.                 if(newValue>this._year){
  69.                     this._year = newValue;
  70.                     this.edition++;
  71.                 }
  72.             }
  73.     }
  74. });
  75.  
  76. book.year = 2007;
  77. console.log(book.year); //2004
  78. console.log(book.edition); //1
  79.  
  80. Object.getOwnPropertyDescriptor(book,"_year");
  81. //Object {value: 2004, writable: false, enumerable: false, configurable: false}
  82. Object.getOwnPropertyDescriptor(book,"edition");
  83. //Object {value: 1, writable: false, enumerable: false, configurable: false}
  84. Object.getOwnPropertyDescriptor(book,"year");
  85. //Object {get: function, set: function, enumerable: false, configurable: false}
  86.  
  87.  
  88.  
  89.  
  90. /*****************************************/
  91. // working and identical to the first example
  92.  
  93. var book = {};
  94. Object.defineProperties(book,{
  95.     _year: {
  96.         value: 2004,
  97.         writable: true,
  98.         enumerable: true,
  99.         configurable: true
  100.     },
  101.     edition: {
  102.         value: 1,
  103.         writable: true,
  104.         enumerable: true,
  105.         configurable: true
  106.     },
  107.  
  108.     year: {
  109.         get: function(){
  110.                 return this._year;
  111.             },
  112.         set: function(newValue){
  113.                 if(newValue>this._year){
  114.                     this._year = newValue;
  115.                     this.edition++;
  116.                 }
  117.             }
  118.     }
  119. });
  120.  
  121. book.year = 2007;
  122. console.log(book.year); // 2007
  123. console.log(book.edition); // 2
  124. book.year = 2009;
  125. console.log(book.edition); // 3
Advertisement
Add Comment
Please, Sign In to add comment