Skip to content

Commit e01a354

Browse files
author
Bradley Meck
committed
closure based prototypes using ephemeron tables
1 parent f09082d commit e01a354

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

example/multiple_inheritence.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var PrototypeChain = function(prototypes) {
3737
}
3838
, "delete": function(ArgInfo) {
3939
}
40-
} )
40+
}, prototypes[prototypes.length-1] )
4141
}
4242
function mySuper() {}
4343
mySuper.prototype.fly = function() {
@@ -61,9 +61,15 @@ function myself(){}
6161
myself.prototype = proto
6262

6363
var me = new myself
64-
me.fly()
65-
me.run()
66-
me.id()
64+
me.fly()//from superduper
65+
me.run()//from super
66+
me.id()//from identity
67+
68+
console.log("Am I an Identity? "+(me instanceof myIdentity))
69+
//only one chain can be used for instanceof T_T
70+
console.log("Am I an Super? "+(me instanceof mySuperduper))
71+
72+
//prototype chain changes are preserved!
6773
mySuper.prototype.run = function() {
6874
console.log("Acting mild mannered I walk")
6975
}

example/sharing_closures.js

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

0 commit comments

Comments
 (0)