callable objects ?

Irakli Gozalishvili rfobic at gmail.com
Tue Apr 3 17:41:07 PDT 2012



On Tuesday, 2012-04-03 at 14:07 , David Bruant wrote:

> 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.
>  


Well I did not suggested it should be backwards compatible, also nor classes nor shorter object syntax is backwards compatible.


> > 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

Also de-sugared version may be provided (and even prototyped today using __proto__):

var Point = Function.create(Object.prototype, function(x, y) {
this.getX = function() { return x };
this.getY = function() { return y };
}, {
toString: function() {
return '<' + this.getX() + ',' + this.getY() + '>'
  }
});
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120403/8840014c/attachment.html>


More information about the es-discuss mailing list