Existential operator (was: ||= is much needed?)

Brendan Eich brendan at mozilla.org
Wed Jun 20 10:57:31 PDT 2012


Brendan Eich wrote:
> Let's look at what CoffeeScript requires to avoid this exception:
>
> $ cat /tmp/z.coffee
> person = {}
> person?.getName?()
>
> $ ./bin/coffee -p /tmp/z.coffee
> (function() {
>   var person;
>
>   person = {};
>
>   if (person != null) {
>     if (typeof person.getName === "function") {
>       person.getName();
>     }
>   }
>
> }).call(this);
>
> $ ./bin/coffee /tmp/z.coffee && echo "no throw"
> no throw 

But of course, to the point that "it's rare to have a maybe-method 
called on a maybe-object", the minimum required by CoffeeScript here is 
just:

$ cat /tmp/z.coffee
person = {}
person.getName?()

$ ./bin/coffee -p /tmp/z.coffee
(function() {
   var person;

   person = {};

   if (typeof person.getName === "function") {
     person.getName();
   }

}).call(this);

$ ./bin/coffee /tmp/z.coffee && echo "no throw"
no throw

/be


More information about the es-discuss mailing list