callable objects ?

David Bruant bruant.d at gmail.com
Tue Apr 3 14:07:06 PDT 2012


Le 03/04/2012 22:00, Irakli Gozalishvili a écrit :
> Here is more or less what I have in mind: https://gist.github.com/2295048
>
> // class
> var Point = {
>   (x, y) {
>     this.getX = { () { return x; } }
>     this.getY = { () { return x; } }
>   }
>   toString() {
>     return '<' + this.getX() + ',' + this.getY() + '>';
>   }
> }
Interesting new function syntax.
The prototype and function body could even be declared together. What
about 'length'?

> Also such callable objects provide shorter alternative to current
> function syntax:
> // shorter than function
> numbers.
>   filter({ (x) { return x % 2 } }).
>   // maybe single expression can be even shorter like arrow functions ?
>   map({ (x) x * x }).
>   forEach({ (x) { this.add(x) } }, that);
+1

> Also this would allow interesting APIs similar to those found in clojure:
>
> // maps / sets similar like in clojure ?
> var map = WeakMap(), key = {}, value = {};
> map.set(key, value);
> map(key) // => value
So far so good.

> key(map) // => value
This cannot work for backward-compat reasons. In ES1-5, "key = {}"
creates a regular (non-callable) object.

> And maybe built-ins could have it for doing `[]` work:
> // Maybe even non-magical replacement for `[]`
> [ 'a', 'b', 'c' ](1) // => 'b'
Again, this is a valid ES1-5 expression and throws an error. Arrays and
function have diverging opinions on the 'length' property.

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120403/2d5ebc4d/attachment.html>


More information about the es-discuss mailing list