We're in a new century now, and its hallmark is humans doing things together, mostly on screens, at scales unimaginable in earlier times.
This was last updated for CoffeeScript 1.7.1.
Imagine that this sentence is a beautifully-crafted, flowing intro paragraph. "Wow, what a great introduction to this CoffeeScript guide," you whisper. Now, let's quickly explore all of CoffeeScript's ways to set default values.
As a function parameter
To quote the CoffeeScript documentation, functions can have "default values for arguments, which will be used if the incoming argument is missing (
undefined)." Here's how you might use it:
shootLaserBeam = (color = "red") -> # ...
Now, if the first parameter is
null, it'll be defined as the string "red". If it's anything else (even falsy values like 0 and the empty string), it won't be set to the default value.
Using the existential operator
CoffeeScript's existential operator can be used to set default values.
sendThreateningMessageFromUFO = (options) -> options.message ?= "I will hold your pathetic planet hostage" # ...
You can also set another variable using a sexier version of a ternary operator.
# this... message = evilMessage ? "Exterminate!" # ...is equivalent to this: if evilMessage? message = evilMessage else message = "Exterminate!" # ...or this: message = if evilMessage? then evilMessage else "Exterminate!"
The ternary operator makes sure that the variable isn't
undefined and isn't
You'll get a compile error for if you try to define not-yet-defined variables. For example, this gives a compiler error:
someUndefinedVariable ?= "I will destroy your worlds"
This makes sense, right? Why set a default value for something that's totally undefined? If you, for some reason, need to do this, you can use the existential operator on the variable:
someUndefinedVariable = "Flee, puny humans" unless someUndefinedVariable?
Discarding falsy values, too
If you want to set defaults for any falsy value (not just
null), you can use
angryAliens = false angryAliens or= true # angryAliens is now true # ||= is totally equivalent to or= ufoCount = 0 ufoCount ||= 50 # ufoCount is now 50 # compare it to the existential operator: numberOfAliens = 0 numberOfAliens ?= 1000 # numberOfAliens is still zero
The important takeaway here:
?= only discards
or= filters any falsy value.
For undefined only
Most of CoffeeScript either tests against falsy values or "is it
undefined". You might want to set things only if it's
undefined and nothing else, not even
null. Here's how you might do that:
cropCircleCount = 69 if cropCircleCount is undefined
null or anything falsy other than
undefined, it'll stay that way.
var isUndefined = window.myFunVariable === undefined; // This fails on older browsers because "undefined" is, well, undefined.
CoffeeScript makes sure that this works in older browsers (by compiling
void 0 to reliably produce an undefined value).
And now, a summary.
- Use default parameters if it's a function parameter. Only sets to default if the value is
?=to set a value to a default if the value is
||=) to reject other falsy values like 0 and the empty string.
Go forth and set your CoffeeScript defaults, friend.
I'm excited for the Glass-Luxottica joint task force. It's a step away from futuristic looking wearable tech and a step towards technological wearbles for which I would dish out that dolla. If wearables are designed to be seamlessly and beautifully integrated into everyday life with all of the consumer base in mind—yes, even the omniscient white male—then I believe Wired's prediction that wearable tech being as big as the smartphone could come true.
I agree; wearables are currently being made for nerds, not for fashionable people.
I like the Node Version Manager for, well, managing Node.js versions. When you install it, it adds a
source to the bottom of your profile (effectively
source nvm.sh). I didn't want it to do that during installation, though. I found myself pondering, "How do I disable that?"
It's pretty easy, it turns out; you just tell it to redirect that source line elsewhere, like so:
curl https://raw.githubusercontent.com/creationix/nvm//install.sh | PROMPT=/dev/null sh
The install script looks for a variable called "PROMPT" and adds the
source line to that, so setting it to
/dev/null adds the line to nothing!
You could also install it by cloning it from Git and doing it manually, but I prefer this because I can be sure I'm getting a stable version without doing a lot of commands.
From About Feminism:
We happen to be humans who loved technology and embrace it wholeheartedly. But it was always clear things would be different if we were male.
I don't think I say "must-read" too often; this is a must-read.