Flash / Flex / ActionScript/Class/Object

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Accessing Object Methods

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript ABC");
        var sSubject:String = sTitle.substr(0, 12);
        trace(sSubject);  // Displays: ActionScript
    }
  }
}



Accessing Object Properties

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("Bible");
        var sName:String = new String("Lott");
        
        trace(sTitle.length);
        
        if(sTitle.length > 12) {
          trace("The title has more than twelve characters.");
        }
    }
  }
}



Creating an Object: var variableName:datatype = new ClassName();

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var oFirstObject:Object = new Object();
        trace(oFirstObject instanceof Object);  // Displays: true
        var sTitle:String = new String("ActionScript Bible");
        trace(sTitle instanceof String);  // Displays: true
    }
  }
}



Removing Properties

 
The delete operator can be used to remove key-value pairs from an associative array.
The delete operator returns true if deletion succeeds. 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var pastas:Object = {t: 2, g: 14, s: 9};
        trace(pastas["s"]); // Displays 9
        delete pastas["s"];
        trace(pastas["s"]); // Displays undefined
    }
  }
}



Testing for Existence

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var notes:Object = {Roger: "hereiam"};
        trace( "Roger" in notes ); // Returns true
        trace( "Josh" in notes); // Returns false
    }
  }
}



Use this operator to investigate instances of classes for methods and public properties

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
         var sprite:Sprite = new Sprite();
         trace("alpha" in sprite); // Displays true
         trace("getChildAt" in sprite); // Displays true
         var person:Person = new Person("Ben");
         trace("name" in person); // Displays true
         trace("SSN" in person); // Displays false
    }
  }
}
 class Person
 {
     public var name:String;
     private var SSN:Number;
     public function Person(name:String)
     {
         this.name = name;
     }
 }



Using Objects as Associative Arrays

 
package{
  import flash.display.Sprite;
  import flash.utils.Dictionary;
  public class Main extends Sprite{
    public function Main(){
        var guy1:Person = new Person("Roger");
        var guy2:Person = new Person("Mims");
        var girl1:Person = new Person("Ashley");
          var notes:Object = new Object();
          notes["Roger"] = "Likes games";
          notes["Mims"] = "Super organized";
          notes["Ashley"] = "Enjoys drawing";
    }
  }
}
class Person{
   public function Person(string:String){
   }
}



Using the dot (.) operator to create the same variable causes an error because it violates the syntactic rules for identifiers:

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var info:Object = new Object(  );
        info["411"] = "Information Line";
        
        var info:Object = new Object(  );
        info.411 = "Information Line";  // ERROR! Identifiers must not start
    }
  }
}