-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsharing_closures.js
More file actions
57 lines (51 loc) · 1.58 KB
/
sharing_closures.js
File metadata and controls
57 lines (51 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//sharing closures can allow prototypal construction speed times while gaining access to a closure
//and not just any closure, all closures to objects of a given type!
var EphemeronTable = require("../lib/overload").EphemeronTable
var MyType
;(function(){
var closures = new EphemeronTable;
MyType = function() {
var myClosures = {views:0}
closures.set(this,myClosures)
}
var communicate = MyType.prototype.communicate = function(object) {
return closures.get(object)
}
MyType.prototype.view = function() {
return communicate(this).views++
}
MyType.prototype.views = function() {
return communicate(this).views
}
MyType.prototype.syncViewsWith = function(obj) {
communicate(this).views = communicate(obj).views
}
})()
var instance_a = new MyType
, instance_b = new MyType
instance_a.view()
instance_b.syncViewsWith(instance_a)
console.log(instance_b.views())//1
//making new prototypes is still possible!
MyType.prototype.doubleView = (function(){
var communicate = MyType.prototype.communicate
return function() {
communicate(this).views += 2
}
})()
instance_a.doubleView()
//since communicate is available you can still access closures until it isnt
console.log(MyType.prototype.communicate(instance_a).views)//3
delete MyType.prototype.communicate
//Communicate is no longer available (kinda like sealing)
try{
console.log(MyType.prototype.communicate(instance_a))//fails
}
catch(e) {
console.log(e.stack)//error!
}
//Compiled functions keep the key!
var instance_c = new MyType
instance_c.syncViewsWith(instance_a)
instance_c.doubleView()
console.log(instance_c.views())//5