ugrás a tartalomhoz

EcmaScript 5 Object.create() Object.defineProperties

Ustak · 2011. Május. 23. (H), 13.38
Üdv mindenkinek, szerintem a kód magáért beszél :-). Hogy oldanátok meg egy hasonló öröklődést az új függvényekkel? Mit nézek el vajon?
  1. //Items hierarchy - all visible (User Interface) items has some properties in common  
  2. var TIB = {};  
  3.   
  4. /* 
  5.  *  TIB.ui.AbstractItem 
  6.  *  Parent of all Visible items 
  7.  * 
  8.  */  
  9.   
  10.   
  11. TIB.ui = {};  
  12. TIB.ui.AbstractItem = {  
  13.     id : null,  
  14.     DOM : {  
  15.        events: {},  
  16.        html : {  
  17.           wrapper : '<div class="tibitemwrapper" >'+  
  18.                         '<h3 class="ui-widget ui-widget-header ui-corner-all ui-border-all"></h3>'+  
  19.                         '<div class="tibitemcontainer ui-widget ui-widget-content ui-corner-all ui-border-all">'+  
  20.                         '</div>'+  
  21.                     '</div>',  
  22.           content : null,  
  23.           classname : "abstract"  
  24.            },  
  25.        style : {},  
  26.         },  
  27.     events : {},  
  28.     enabled : true,  
  29.     properties : {  
  30.         name : "TIB.ui.AbstractItem"  
  31.         },  
  32.     value : null  
  33. }  
  34.   
  35.   
  36. /* 
  37.  *  TIB.ui.AbstractTextItem 
  38.  *  Parent of all Visisible Items that has Text on it. 
  39.  * 
  40.  */  
  41.   
  42. TIB.ui.AbstractTextItem = Object.create(TIB.ui.AbstractItem,{  
  43.     toString : { value : function() {  
  44.         return this.id+", "+this.properties.name+", "+this.properties.fontSize+", "+this.DOM.html.classname+", "+this.value;  
  45.         },  
  46.     },  
  47.   
  48.     properties : {  
  49.   
  50.         value : {  
  51.             name : "TIB.ui.AbstractTextItem",  
  52.             fontSize : "1em"  
  53.         }  
  54.       
  55.     }  
  56. });  
  57.   
  58. TIB.ui.TextArea = Object.create(TIB.ui.AbstractTextItem);  
  59. Object.defineProperties(TIB.ui.TextArea,{  
  60.   
  61.     properties : {  
  62.         value :  {  
  63.             name : "TIB.ui.TextArea",  
  64.             fontSize : "2em"  
  65.         },  
  66.     },  
  67.     value : {  
  68.         value : "SzövegDoboz"   
  69.     },  
  70.   
  71.   
  72. });  
  73. console.log(TIB.ui.AbstractTextItem.toString()); //classname : abstract  
  74. console.log(TIB.ui.TextArea.toString()); //classname abstract  
  75. TIB.ui.TextArea.DOM.html.classname = "textareaclassname";  
  76. console.log(TIB.ui.AbstractTextItem.toString()); //classname textareaclassname !!!!!  
  77. console.log(TIB.ui.TextArea.toString()); //classname textareaclassname  
Köszi a véleményeket, üdv:
Gábor
 
1

Erre lyukadtam ki:

Ustak · 2011. Május. 23. (H), 16.09
  1. "use strict";  
  2.   
  3.   
  4.   
  5. //hopefully we have these implemented natively, but if not...  
  6.   
  7. if (Object.defineProperty && Object.create) {  
  8.     if (!Object.defineProperties) {  
  9.         Object.defineProperties = function(obj, props) {  
  10.             for (var prop in props) {  
  11.                 Object.defineProperty(obj,prop,props[prop]);  
  12.             }  
  13.         }  
  14.         console.log("Object.defineProperties function created");  
  15.     }  
  16. else {  
  17.     alert("unsupported browser");  
  18. }  
  19.   
  20.   
  21.   
  22. //Items hierarchy - all visible (User Interface) items has some properties in common  
  23. var TIB = {};  
  24.   
  25. /* 
  26.  *  TIB.ui.AbstractItem 
  27.  *  Parent of all Visible items 
  28.  * 
  29.  */  
  30.   
  31.   
  32. TIB.ui = {};  
  33. TIB.ui.AbstractItem = {  
  34.     id : null,  
  35.     DOM_html_wrapper : '<div class="tibitemwrapper" >'+  
  36.                         '<h3 class="ui-widget ui-widget-header ui-corner-all ui-border-all"></h3>'+  
  37.                         '<div class="tibitemcontainer ui-widget ui-widget-content ui-corner-all ui-border-all">'+  
  38.                         '</div>'+  
  39.                     '</div>',  
  40.     DOM_html_content : null,  
  41.     DOM_html_classname : "abstract",  
  42.     DOM_html_style : null,  
  43.     DOM_html_events : {},  
  44.     events : {},  
  45.     enabled : true,  
  46.     properties_name : "TIB.ui.AbstractItem",  
  47.     value : null  
  48. }  
  49.   
  50.   
  51. /* 
  52.  *  TIB.ui.AbstractTextItem 
  53.  *  Parent of all Visisible Items that has Text on it. 
  54.  * 
  55.  */  
  56.   
  57. TIB.ui.AbstractTextItem = Object.create(TIB.ui.AbstractItem,{  
  58.     toString : { value : function() {  
  59.         return this.id+", "+this.properties_name+", "+this.properties_fontsize+", "+this.DOM_html_classname+", "+this.value;  
  60.         },  
  61.     },  
  62.   
  63.     properties_name : { value :  "TIB.ui.AbstractTextItem" },  
  64.     properties_fontsize : { value : "1em" }  
  65.       
  66. });  
  67.   
  68. TIB.ui.TextArea = Object.create(TIB.ui.AbstractTextItem);  
  69. Object.defineProperties(TIB.ui.TextArea,{  
  70.   
  71.     properties_name :  { value : "TIB.ui.TextArea" },  
  72.     properties_fontsize :  { value : "2em" },  
  73.     value :  { value : "SzövegDoboz"  }  
  74.   
  75. });  
  76. console.log(TIB.ui.AbstractTextItem.toString());  
  77. console.log(TIB.ui.TextArea.toString()); //abstract  
  78. TIB.ui.TextArea.DOM_html_classname = "textareaclassname";  
  79. console.log(TIB.ui.AbstractTextItem.toString()); //abstract  
  80. console.log(TIB.ui.TextArea.toString()); //textareaclassname  
Úgy tűnik, rekurzívan kellene csinálni, de úgy meg félek túl sok az új object. Szerintem ennél maradok, furcsa de jó.
Persze várom a véleményeket :-)
Üdv:
Gábor
2

Kár hogy erre senki sem

bb0072 · 2011. Május. 24. (K), 10.23
Kár hogy erre senki sem tudott valami okosat mondani, mert érdekes a felvetett a probléma. FF-ben és Chrome-ban is ugyanez a helyzet, tehát nem valami bug-ról van szó egyik vagy másik böngészőben. Így elég nehézkesen használható ez az egész koncepció.