Created
April 3, 2012 19:43
-
-
Save Gozala/2295048 to your computer and use it in GitHub Desktop.
Daydreaming of callable objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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