Zurück   Flashforum > Flash > ActionScript > ActionScript 1

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 02-04-2004, 10:44   #1 (permalink)
explore. relax. enjoy.
 
Benutzerbild von Yobert
 
Registriert seit: Nov 2002
Ort: Hamburg
Beiträge: 96
Umgesetzte Idee: Kopie von einem Objekt. Egal von welchem.

Hi,

ich benötigte für ein Projekt die Möglichkeit, Objekte, Arrays, Strings, etc. zu kopieren. Auch wenn diese Typen-gemischt und verschachtelt sind.

Hier ist meine Umsetzung:

ActionScript:
  1. // Object copy
  2. Object.prototype.copy = function () {
  3.    
  4.     // if this has a length
  5.     // it must be an array, for objects have no length
  6.     if (this.length != undefined) return this.toArray ();
  7.    
  8.     // this is an object
  9.     else {
  10.        
  11.         // create result object holder
  12.         var result = {};
  13.        
  14.         // copy all nested objects recursively
  15.         for (obj in this) result[obj] = this[obj].copy ();
  16.        
  17.         // return result
  18.         return result;
  19.        
  20.     }
  21.    
  22. };
  23.  
  24. // String copy
  25. String.prototype.copy = function () {
  26.    
  27.     // return String
  28.     return String (this);
  29.    
  30. };
  31.  
  32. // Number copy
  33. Number.prototype.copy = function () {
  34.    
  35.     // return Number
  36.     return Number (this);
  37.    
  38. };
  39.  
  40. // Boolean copy
  41. Boolean.prototype.copy = function () {
  42.    
  43.     // return evaluated boolean
  44.     return Boolean (Number (this));
  45.    
  46.     // Comment:
  47.     // the Number () process is necessary,
  48.     // because if it is not used, it would return true every time
  49.    
  50. };
  51.  
  52. // Array convert and copy
  53. // IMPORTANT:
  54. // This method forces an array conversion!
  55. // Even if the contents are not Array-like.
  56. Object.prototype.toArray = function () {
  57.    
  58.     // create result array holder
  59.     var result = [];
  60.    
  61.     // count results
  62.     var resultCount = 0;
  63.    
  64.     // browse through all nested objects
  65.     for (obj in this) {
  66.        
  67.         // convert and copy all nested arrays recursively
  68.         if (typeof (this[obj]) == "object" && this[obj].length) result[obj] = this[obj].toArray ();
  69.        
  70.         // convert all other non-arrays
  71.         else result[obj] = this[obj].copy ();
  72.        
  73.         // increase result count
  74.         resultCount++;
  75.     }
  76.    
  77.     // if we don't have any results / nested objects
  78.     // include this into an Array
  79.     // and return this Array
  80.     if (resultCount == 0) return Array (this);
  81.    
  82.     // return result
  83.     return result;
  84.    
  85. };
  86. // hide methods
  87. ASSetPropFlags (Object.prototype, ["copy","toArray"], 1);
  88. ASSetPropFlags (String.prototype, ["copy"], 1);
  89. ASSetPropFlags (Number.prototype, ["copy"], 1);
  90. ASSetPropFlags (Boolean.prototype, ["copy"], 1);
  91.  
  92. // Test
  93.  
  94. mixed = {};
  95. mixed.obj1 = {};
  96. mixed.obj1.obj = {};
  97. mixed.obj1.obj.str1 = "Object1/Object/String1";
  98. mixed.obj1.obj.str2 = "Object1/Object/String2";
  99. mixed.obj1.obj.arr = [];
  100. mixed.obj1.obj.arr[0] = ["Object1/Object/Array/0/0:String", 0, false, {str: "Object1/Object/Array/0/3:Object/String"}];
  101. mixed.obj1.obj.arr[1] = ["Object1/Object/Array/1/0:String", 1, true];
  102. mixed.obj1.obj.arr.nonArray = ["Object1/Object/Array/NonArray", 2, true];
  103. mixed.obj2 = {};
  104. mixed.obj2.str1 = "Object2/String1";
  105. mixed.obj2.str2 = "Object2/String2";
  106. mixed.obj2.num = 23;
  107. mixed.bool = false;
  108.  
  109. mixedCopy = mixed.copy ();
  110.  
  111. numArray = Number (17).toArray ();

Zum Kommentieren, Kritisieren, Korrigieren und Kopieren freigegeben.

Gruß

Yobert
__________________
www.what-we-need.com

Geändert von Yobert (02-04-2004 um 10:47 Uhr)
Yobert ist offline   Mit Zitat antworten
Alt 02-04-2004, 16:09   #2 (permalink)
explore. relax. enjoy.
 
Benutzerbild von Yobert
 
Registriert seit: Nov 2002
Ort: Hamburg
Beiträge: 96
Kein einziger Kommentar bislang? Na sowas...
__________________
www.what-we-need.com
Yobert ist offline   Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind an
Pingbacks sind an
Refbacks sind an



Alle Zeitangaben in WEZ +1. Es ist jetzt 07:53 Uhr.

Domains, Webhosting & Vserver von Host Europe
Unterstützt das Flashforum!
Adobe User Group


Copyright ©1999 – 2012 Marc Thiele