Flash / Flex / ActionScript/Language/Comparison Operators

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

Checking Equality or Comparing Values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(5 == 6);    // Displays: false
        trace(6 == 6);    // Displays: true

    }
  }
}



Comparison Operators in Actionscript

 
Operator      Name 
==      Equals 
!=      Not equals 
>      Greater than 
<      Less than 
>=      Greater than or equal 
<=      Less than or equal



Equality (==)

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var weather:String = "rain";
        
        if (weather == "rain") {
           trace("bringUmbrella");
        }
    }
  }
}



Greater Than (>) and Less Than (

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var x:int = 0;
        
        var a:int = -1;
        var b:int = 0;
        var c:int = 1;
        
        trace(x <= a); // Displays: false
        trace(x >= a); // Displays: true
        trace(x <= b); // Displays: true
        trace(x >= b); // Displays: true
        trace(x <= c); // Displays: true
        trace(x >= c); // Displays: false
    }
  }
}



Not Equal To (!=)

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var weather:String = "rain";
        
        
        if (weather != "sun") { trace("bringUmbrella"); }
        if (!(weather == "sun")) { trace("bringUmbrella"); }
        
    }
  }
}



Number Comparison

 
 
Expression      Result 
6 == 6          true 
6 != 6          false 
6 > 6           false 
6 < 6           false 
6 >= 6          true 
6 <= 6          true



Strict equality

 
!==      Strict inequality 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var myVariable:Number = 999;
        if (myVariable == 4){
        trace("if condition true");
        }
        trace(myVariable);
    }
  }
}



The logical inequality operator (!=) returns false if two values are equal and true if they aren"t.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(5 != 6);    // Displays: true
        trace(6 != 6);    // Displays: false
    }
  }
}



Two composite items are equal only if they both refer to the identical object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var arrayOne:Array = new Array("a", "b", "c");
        
        var arrayTwo:Array = arrayOne;
        trace(arrayOne == arrayTwo);          // Displays: true

    }
  }
}



Use the <= and >= operators to check if one value is less than or equal to, or greater than or equal to, another value:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(5 <= 6);   // Displays: true
        trace(5 >= 5);   // Displays: true
    }
  }
}



Use the < and > operators to check if one value is less than or greater than another value:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(5 < 6);    // Displays: true
        trace(5 > 5);    // Displays: false
    }
  }
}



When comparing primitive datatypes, ActionScript compares them by value.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var quantity:Number = 6;
        var total:Number = 6;
        trace (quantity == total);         // Displays: true
    }
  }
}



When you compare composite datatypes, ActionScript compares them by reference.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var arrayOne:Array = new Array("a", "b", "c");
        var arrayTwo:Array = new Array("a", "b", "c");
        trace(arrayOne == arrayTwo);          // Displays: false
    }
  }
}