-
Notifications
You must be signed in to change notification settings - Fork 6
is Operator
Shane Brinkman-Davis Delamore edited this page Mar 29, 2018
·
4 revisions
related: instanceof operator
In CaffeineScript, the is
operator is a little different from CoffeeScript. You can think of it as: "A equals-or-is-typeof B". isnt
is the same as !(a is b)
.
-
a is b
istrue
iff one of these are true:-
a == b
(JavaScript ===) a.constructor == b
-
This effectively obsoletes JavaScripts broken typeof
operator.
"hi" is String # true. The constructor of "" is indeed String
(->) is Function # true
[] is Object # false - yay! The constructor of an array is not Object
[] is Array # true - yay!
null is Object # false - yay! No way the constructor of a null is Object
null is undefined # false
undefined is null # false
class Foo
foo = new Foo
foo is Foo # true - yay!
a = null
a is null # true (since values match, not because of constructor equality)
1 is 1 # true
1 is Number # true
{} is {} # false - no deep equality
b = {}
b is b # true - for any value of b
Type = Array
value = []
value is Type # true
Note: The equality-aspect of 'is' was maintained from CoffeeScript because null is null
requires equality testing, instead of constructor-based testing. Once we've allowed that, it seems illogical to not also allow 1 is 1
to be true.
I'd really like to support an is-class test, but JavaScript doesn't have an efficient way to test if something is a class. Further, Class
isn't defined, unlike all the other types above.
- Home
- Get Started
- Benefits
- Highlights
- Productivity by Design
- CaffeineScript Design
- What is CaffeineScript Good For?
- Get the most out of JavaScript
- Language Comparison
- CHANGELOG
- Blocks Instead of Brackets
- Binary Line-Starts
- Everything Returns a Value
- Streamlined Modules
- Scopes and Variables
- Optional Commas
- Semantics
- Ambiguities