Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created April 3, 2012 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gozala/2295048 to your computer and use it in GitHub Desktop.
Save Gozala/2295048 to your computer and use it in GitHub Desktop.
Daydreaming of callable objects.
// 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)
// 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);
// 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);
}
});
// maps / sets similar like in clojure ?
var map = WeakMap(), key = {}, value = {};
map.set(key, value);
map(key) // => value
key(map) // => value
// Maybe even non-magical replacement for `[]`
[ 'a', 'b', 'c' ](1) // => 'b'
({ a: 1, b: 2 })('a') // => 1
('b')({ a: 1, b: 2 }) // => 2
Copy link

ghost commented May 2, 2012

I made a "makeCallable" with proxies. Although it's pretty trivial to do I figured I'd mention it for any passerbys!
https://github.com/Benvie/meta-objects/blob/master/lib/callable.js
part of https://github.com/Benvie/meta-objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment