callable objects ?

Irakli Gozalishvili rfobic at gmail.com
Tue Apr 3 17:48:49 PDT 2012


Also I just realized that I have not mentioned it but I meant that new Point(0, 0) would do following:

var point = Object.create(Point);
var value = Function.apply.call(point, arguments);
return typeof(value) === 'undefined' ? point : value;

Regards
--
Irakli Gozalishvili
Web: http://www.jeditoolkit.com/


On Tuesday, 2012-04-03 at 17:44 , Irakli Gozalishvili wrote:

>  
>  
> On Tuesday, 2012-04-03 at 13:47 , Axel Rauschmayer wrote:
>  
> > I think you should make the distinction between [[Call]] and [[Construct]].  
> >  
> >  
>  
>  
> You can do it via (this insntanceof ..) already and dispatch to call / construct if you want.  
>   
> > But both would be great to have for objects. It’s probably best to create importable names.
> >  
>  
> Importable names won't allow shorter than function form though, so I think just () {} is better.  
>   
> > Then making the `new` operator available to object exemplars becomes indeed simple. For example:
> >  
> >     // Root object exemplar
> >     import Construct from somewhere;
> >     let ObjectExemplar = {
> >         [Construct](...args) {
> >             let inst = Object.create(this);
> >             let result = inst.init(...args);
> >             return (result === undefined ? inst : result);
> >         }
> >     }
> >  
> >  
> >  
> > On Apr 3, 2012, at 22:00 , Irakli Gozalishvili wrote:
> > > Hi,  
> > >  
> > > Please excuse me if this will sound too crazy, but this idea have being growing in my head for so long that I can't stop myself from proposing it here. I have experimented many different ways of doing inheritance in JS:
> > >  
> > > 1. Starting with a simple sugar that reduces machinery involved, similar to backbone.js
> > > https://github.com/Gozala/extendables
> > >  
> > > 2. Finishing with class free prototypal inheritance
> > > https://github.com/Gozala/selfish
> > >  
> > > I have to say that experience with selfish turned out very interesting, it made things so much simpler no special forms, no twisted relations between constructors and objects, just a plain objects. Most of these things are not obvious until you start using them, but the fact that exemplars (classes) are no different from regular objects in the system makes things much simpler.  
> > >  
> > > That being said I have also came to realize that in most of the cases functions as exemplars would be a better feet than objects. As a matter of fact if objects could be made callable I think it could could have replaced most of the things that classes are targeting in more elegant way.  
> > >  
> > > 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() + '>';
> > >   }
> > > }
> > >  
> > > var a = new Point(0, 0)
> > > var b = new Point(1, 7)
> > >  
> > > // Examples from class proposal
> > >  
> > > // extend is like create with a diff that second arg is an object.
> > > var SkinnedMesh = Object.extend(THREE.Mesh, {
> > >   (geometry, materials) {
> > >     // call the superclass constructor
> > >     THREE.Mesh.call(this, geometry, materials);
> > >      
> > >     // initialize instance properties
> > >     this.identityMatrix = new THREE.Matrix4();
> > >     this.bones = [];
> > >     this.boneMatrices = [];
> > >  
> > >     // ...
> > >   }
> > >  
> > >   update (camera) {
> > >     THREE.Mesh.update.call(this);
> > >   }
> > > });
> > >  
> > > 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);
> > >  
> > >  
> > > 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
> > > key(map) // => value
> > >  
> > >  
> > > And maybe built-ins could have it for doing `[]` work:
> > >  
> > >  
> > > // Maybe even non-magical replacement for `[]`
> > >  
> > > [ 'a', 'b', 'c' ](1) // => 'b'
> > > ({ a: 1, b: 2 })('a') // => 1
> > > ('b')({ a: 1, b: 2 }) // => 2
> > >  
> > >  
> > >  
> > >  
> > > Regards
> > > --
> > > Irakli Gozalishvili
> > > Web: http://www.jeditoolkit.com/
> > >  
> > > _______________________________________________
> > > es-discuss mailing list
> > > es-discuss at mozilla.org (mailto:es-discuss at mozilla.org)
> > > https://mail.mozilla.org/listinfo/es-discuss
> >  
> > --  
> > Dr. Axel Rauschmayer
> > axel at rauschma.de (mailto:axel at rauschma.de)
> >  
> > home: rauschma.de (http://rauschma.de)twitter: twitter.com/rauschma (http://twitter.com/rauschma)
> > blog: 2ality.com (http://2ality.com)
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
>  

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


More information about the es-discuss mailing list