-
Notifications
You must be signed in to change notification settings - Fork 103
Operators
kazuho edited this page Jul 10, 2012
·
21 revisions
The operators of JSX are the same to those in JavaScript (ECMA-262 3rd edition) except for the following changes.
- types of the operands accepted by the operators are more restrictive
- logical operators (&& ||) return boolean
- binary ?: operator has been introduced (to cover the use of || in JavaScript to return non-boolean values)
- introduction of the as operator
- delete is a statement instead of an operator
- a wrapper object (Boolean, Number, String) cannot become a left-hand-side expression
The table below lists the operators supported by JSX.
Operator | Returned Type | Operand Type(s) | Note |
---|---|---|---|
(x) | typeof x | grouping operator | |
(...) | returned type of the called function | function call | |
x.property | typeof x.property | x:Object | |
array[index] | Nullable.<T> | array: Array.<T>, index: number | |
map[key] | Nullable.<T> | map: Map.<T>, key: string | |
x++ | typeof x | number or int | |
x-- | typeof x | number or int | |
x instanceof y | boolean | x: Object, y: Class, Interface, Mixin | |
x as type | type | cast operator | |
x as __noconvert__ type | type | cast operator (without run-time type check) | |
++x | typeof x | number or int | |
--x | typeof x | number or int | |
+x | typeof x | number or int | |
-x | typeof x | number or int | |
~x | int | number or int | |
! x | boolean | any | |
typeof x | boolean | variant | |
x * y | number or int | number, int, Number | int is returned if both operands are int |
x / y | number or int | number, int, Number | int is returned if both operands are int |
x % y | number | number, int, Number | |
x + y | string | string or String | |
x + y | number or int | number, int, Number | int is returned if both operands are int |
x - y | number or int | number, int, Number | int is returned if both operands are int |
x << y | int | number, int, Number | |
x >> y | int | number, int, Number | |
x >>> y | int | number, int, Number | |
x < y | boolean | number, int, string, Number, String | |
x <= y | boolean | number, int, string, Number, String | |
x > y | boolean | number, int, string, Number, String | |
x >= y | boolean | number, int, string, Number, String | |
x in y | boolean | x: string, y: Map.<T> | |
x == y | boolean | boolean, number, int, string, Object | |
x != y | boolean | boolean, number, int, string, Object | |
x & y | int | number, int, Number | |
x ^ y | int | number, int, Number | |
x | y | int | number, int, Number | |
x && y | boolean | any | |
x || y | boolean | any | |
x ? y : z | typeof y | returned types of y and z should be equal | |
x ?: y | typeof x | returned types of x and y should be equal | |
x = y | typeof x | type of y should be convertible to type of x | |
x op= y | typeof x | same as op, except that x cannot be a wrapper object | op can be any of: * / % + - << >> >>> & ^ | |
x, y | typeof y |
The as operator is used for: conversion between primitive types (including Nullable and variant), down-casting of object types.
The conversion rules between primitive types are defined as follows. If the source type is a Nullable type and if the value is null, a run-time exception is raised under debug builds. The behavior is unspecified for release builds.
The result of conversion from a variant type depends on the result of the typeof operator applied to the variant.
Down-casting of an object type returns a reference to the casted object if successful, otherwise null.
Source Type | Destination Type | Result |
---|---|---|
boolean | number | 0 if false, 1 if true |
boolean | int | same as above |
boolean | string | "false" if false, "true" if true |
number | boolean | false if 0 or NaN, otherwise true |
number | int | fractional part is removed, becomes 0 if NaN, may get rounded to between -231 and 231-1 |
number | string | converted to string representation |
int | boolean | false if 0, otherwise true |
int | number | converted to number of same value |
int | string | converted to string representation |
string | boolean | false if the string is empty, otherwise true |
string | number | 0 if the string is empty, a number if the string can be parsed as a string, otherwise NaN |
string | int | equivalent to: as number as int |
Result of typeof(variant) | Destination Type | Result |
---|---|---|
"undefined" | boolean | false |
"undefined" | number | NaN |
"undefined" | int | 0 |
"undefined" | string | "undefined" |
"null" | boolean | false |
"null" | number | 0 |
"null" | int | 0 |
"null" | string | "null" |
"boolean" | any primitive type | equivalent to the result of: boolean as type |
"number" | any primitive type | equivalent to the result of: number as type |
"string" | any primitive type | equivalent to the result of: string as type |
"object" | any primitive type | depends on the actual type of the value |
"object" | any object type | reference to the object if the value is an object of the specified type, otherwise null |