









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Ian Holyer Ian Holyer
โ (^) http://www.cs.bris.ac.uk/Teaching/Resources/COMSM0104/lectures/http://www.cs.bris.ac.uk/Teaching/Resources/COMSM0104/lectures/
beach
tree
spade spade
link
south
link()
โ (^) the functions get and count can be called from anywhere, not just as methods on counter objects โ (^) the fields are public โ (^) the methods must put this. in front of the fields all the time โ (^) each object has its own method pointers, so if there are a lot of methods, and a lot of objects, there could be space problems
function Counter() { this.n = 0 this.get = get this.count = count function get() { return this.n; } function count() { this.n++; } } ... var c = new Counter();
function Counter() { this.n = 0 this.get = get this.count = count function get() { return this.n; } function count() { this.n++; } } ... var c = new Counter();
โ (^) it is an obscure and little-known technique involving closures that is difficult to find out about in books or tutorials โ (^) there is a potential space problem, because each object has another closure object associated with it behind the scenes
function Counter() { this.n = 0 if (Counter.classCreated) return Counter.classCreated = true Counter.prototype.get = get Counter.prototype.count = count function get() { return this.n; } function count() { this.n++; } } ... var c = new Counter();
function Counter() { this.n = 0 if (Counter.classCreated) return Counter.classCreated = true Counter.prototype.get = get Counter.prototype.count = count function get() { return this.n; } function count() { this.n++; } } ... var c = new Counter();
โ (^) That appears to work in JavaScript, but doesn't really, because an object doesn't have any identity (such as its pointer address) that can be used as a hash value โ (^) So, in the JavaScript implementation, an Entity has an explicit name (id) plus a comment to say it is only for load/save/debug