From bf25e9da958ed6007b75bfff7dc40a258cc66767 Mon Sep 17 00:00:00 2001 From: Tim Caswell Date: Fri, 8 Oct 2010 22:39:35 -0700 Subject: [PATCH] Write code and diagrams for object-graphs-2. --- articles/object-graphs-2.markdown | 54 +++++++++++++++++++++++++ articles/object-graphs-2/classical.dot | 45 +++++++++++++++++++++ articles/object-graphs-2/classical.js | 28 +++++++++++++ articles/object-graphs-2/factory.dot | 44 ++++++++++++++++++++ articles/object-graphs-2/factory.js | 40 ++++++++++++++++++ articles/object-graphs-2/prototypal.dot | 43 ++++++++++++++++++++ articles/object-graphs-2/prototypal.js | 32 +++++++++++++++ 7 files changed, 286 insertions(+) create mode 100644 articles/object-graphs-2.markdown create mode 100644 articles/object-graphs-2/classical.dot create mode 100644 articles/object-graphs-2/classical.js create mode 100644 articles/object-graphs-2/factory.dot create mode 100644 articles/object-graphs-2/factory.js create mode 100644 articles/object-graphs-2/prototypal.dot create mode 100644 articles/object-graphs-2/prototypal.js diff --git a/articles/object-graphs-2.markdown b/articles/object-graphs-2.markdown new file mode 100644 index 0000000..ea0ed16 --- /dev/null +++ b/articles/object-graphs-2.markdown @@ -0,0 +1,54 @@ +Title: Learning Javascript with Object Graphs (Part II) +Author: Tim Caswell +Date: Wed Oct 06 2010 09:31:21 GMT-0700 (PDT) +Node: v0.2.3 + +The first article using graphs to describe JavaScript semantics was so popular that I've decided to try the technique with some more advanced ideas. In this article I'll explain three common techniques for creating objects. They are constructor with prototype, pure prototypal, and object factory. + +My goal is that this will help people understand the strengths and weaknesses of each technique and understand what's really going on. + +## Classical JavaScript Constructors + + + + + + + +![classical](object-graphs-2/classical.dot) + +
+ +## Pure Prototypal Objects + + + + + + + +![classical](object-graphs-2/prototypal.dot) + +
+ +## Object Factories + + + + + + // Output + View now has 5 + View now has 6 + View now has 5 + Saving value 5 somewhere + Now hiding view + + +![classical](object-graphs-2/factory.dot) + +
+ +## Conclusion + +There is so much more I want to explore, but I like to keep these articles somewhat short and bite-size. If there is demand, I'll write a part three explaining how to do ruby-style mixins and other advanced topics. \ No newline at end of file diff --git a/articles/object-graphs-2/classical.dot b/articles/object-graphs-2/classical.dot new file mode 100644 index 0000000..1b89f6c --- /dev/null +++ b/articles/object-graphs-2/classical.dot @@ -0,0 +1,45 @@ +digraph finite_state_machine { + size="8.3,8" + rankdir = "LR" + + /* Execution Contexts */ + node [shape=Mrecord, fillcolor=beige, style=filled]; + top [label="<__proto__>[ Scope ]|Rectangle|rect|Square|sqr"]; + + /* Normal Objects */ + node [shape = Mrecord, fillcolor=lightskyblue, style=filled]; + ob_RectProto [label = "<__proto__>[ Object ]|constructor|getArea|getPerimeter|toString"]; + ob_SquareProto [label = "<__proto__>[ Object ]|constructor|getPerimeter"]; + ob_rect [label = "<__proto__>[ Object ]|width=6|height=4"]; + ob_sqr [label = "<__proto__>[ Object ]|width=5|height=5"]; + + /* Function Objects */ + node [shape = Mrecord, fillcolor=orange, style=filled]; + fn_Rectangle [label="<__proto__>[Rectangle(width, height)]|\{this.width = width…\}|prototype"]; + fn_Square [label="<__proto__>[Square(width)]|\{this.width = width…\}|prototype"]; + fn_getArea [label="<__proto__>[getArea()]|\{return this.width * this.height\}"]; + fn_getPerimeter [label="<__proto__>[getPerimeter()]|\{return 2 * (this.width + this.height)\}"]; + fn_toString [label="<__proto__>[toString()]|\{return this.constructor.name…\}"]; + fn_getPerimeter2 [label="<__proto__>[getPerimeter()]|\{return 4 * this.width\}"]; + + /* References */ + top:Rectangle -> fn_Rectangle:__proto__; + top:Square -> fn_Square:__proto__; + top:rect -> ob_rect:__proto__; + top:sqr -> ob_sqr:__proto__; + fn_Rectangle:prototype -> ob_RectProto:__proto__; + fn_Square:prototype -> ob_SquareProto:__proto__; + ob_RectProto:constructor -> fn_Rectangle:__proto__; + ob_RectProto:getArea -> fn_getArea:__proto__; + ob_RectProto:getPerimeter -> fn_getPerimeter:__proto__; + ob_RectProto:toString -> fn_toString:__proto__; + ob_SquareProto:constructor -> fn_Square:__proto__; + ob_SquareProto:getPerimeter -> fn_getPerimeter2:__proto__; + + /* Inheritance Chains */ + edge [style=dashed] + ob_rect:__proto__ -> ob_RectProto:__proto__; + ob_sqr:__proto__ -> ob_SquareProto:__proto__; + ob_SquareProto:__proto__ -> ob_RectProto:__proto__; + +} \ No newline at end of file diff --git a/articles/object-graphs-2/classical.js b/articles/object-graphs-2/classical.js new file mode 100644 index 0000000..673d661 --- /dev/null +++ b/articles/object-graphs-2/classical.js @@ -0,0 +1,28 @@ +//rectangle +function Rectangle(width, height) { + this.width = width; + this.height = height; +} +Rectangle.prototype.getArea = function getArea() { + return this.width * this.height; +}; +Rectangle.prototype.getPerimeter = function getPerimeter() { + return 2 * (this.width + this.height); +}; +Rectangle.prototype.toString = function toString() { + return this.constructor.name + " a=" + this.getArea() + " p=" + this.getPerimeter(); +}; +//square +function Square(side) { + this.width = side; + this.height = side; +} +Square.prototype.__proto__ = Rectangle.prototype; +Square.prototype.getPerimeter = function getPerimeter() { + return this.width * 4; +}; +//test +var rect = new Rectangle(6, 4); +var sqr = new Square(5); +console.log(rect.toString()) +console.log(sqr.toString()) diff --git a/articles/object-graphs-2/factory.dot b/articles/object-graphs-2/factory.dot new file mode 100644 index 0000000..c257beb --- /dev/null +++ b/articles/object-graphs-2/factory.dot @@ -0,0 +1,44 @@ +digraph finite_state_machine { + size="8.3,8" + rankdir = "LR" + + /* Execution Contexts */ + node [shape=Mrecord, fillcolor=beige, style=filled]; + top [label="<__proto__>[ Scope ]|Controller|on"]; + closure [label="<__proto__>[ Scope ]|view|model"]; + + /* Normal Objects */ + node [shape = Mrecord, fillcolor=lightskyblue, style=filled]; + ob_view [label="<__proto__>[ Object ]|update|close"] + ob_model [label="<__proto__>[ Object ]|value=5|save"]; + ob_on [label="<__proto__>[ Object ]|up|down|save"]; + + /* Function Objects */ + node [shape = Mrecord, fillcolor=orange, style=filled]; + fn_update [label="<__proto__>[update(value)]|\{ console.log(…) \}|<__scope__>[scope]"] + fn_close [label="<__proto__>[close()]|\{ console.log(…) \}|<__scope__>[scope]"] + fn_save [label="<__proto__>[save()]|\{ console.log(…) \}"]; + fn_Controller [label="<__proto__>[Controller(model, view)]|\{ view.update(model.value) … \}"] + fn_onUp [label="<__proto__>[onUp()]|\{ model.value++ … \}|<__scope__>[scope]"]; + fn_onDown [label="<__proto__>[onDown()]|\{ model.value-- … \}|<__scope__>[scope]"]; + fn_onSave [label="<__proto__>[onSave()]|\{ model.save() … \}|<__scope__>[scope]"]; + + /* References */ + ob_view:update -> fn_update:__proto__; + ob_view:close -> fn_close:__proto__; + ob_model:save -> fn_save:__proto__; + top:Controller -> fn_Controller:__proto__; + top:on -> ob_on; + ob_on:up -> fn_onUp:__proto__; + ob_on:down -> fn_onDown:__proto__; + ob_on:save -> fn_onSave:__proto__; + fn_onUp:__scope__ -> closure; + fn_onDown:__scope__ -> closure; + fn_onSave:__scope__ -> closure; + closure:view -> ob_view:__proto__; + closure:model -> ob_model:__proto__; + + /* Inheritance Chains */ + edge [style=dashed] + closure -> top:__proto__; +} \ No newline at end of file diff --git a/articles/object-graphs-2/factory.js b/articles/object-graphs-2/factory.js new file mode 100644 index 0000000..08132f5 --- /dev/null +++ b/articles/object-graphs-2/factory.js @@ -0,0 +1,40 @@ +//controller +function Controller(model, view) { + view.update(model.value); + return { + up: function onUp(evt) { + model.value++; + view.update(model.value); + }, + down: function onDown(evt) { + model.value--; + view.update(model.value); + }, + save: function onSave(evt) { + model.save(); + view.close(); + } + }; +} +//usage +var on = Controller( + // Inline a mock model + { + value: 5, + save: function save() { + console.log("Saving value " + this.value + " somewhere"); + } + }, + // Inline a mock view + { + update: function update(newValue) { + console.log("View now has " + newValue); + }, + close: function close() { + console.log("Now hiding view"); + } + } +); +setTimeout(on.up, 100); +setTimeout(on.down, 200); +setTimeout(on.save, 300); diff --git a/articles/object-graphs-2/prototypal.dot b/articles/object-graphs-2/prototypal.dot new file mode 100644 index 0000000..261932e --- /dev/null +++ b/articles/object-graphs-2/prototypal.dot @@ -0,0 +1,43 @@ +digraph finite_state_machine { + size="8.3,5" + rankdir = "LR" + + + /* Execution Contexts */ + node [shape=Mrecord, fillcolor=beige, style=filled]; + top [label="<__proto__>[ Scope ]|Rectangle|rect|Square|sqr"]; + + /* Normal Objects */ + node [shape = Mrecord, fillcolor=lightskyblue, style=filled]; + ob_Rectangle [label = "<__proto__>[ Object ]|name=\"Rectangle\"|getArea|getPerimeter|toString"]; + ob_Square [label = "<__proto__>[ Object ]|name=\"Square\"|getArea|getPerimeter"]; + ob_rect [label = "<__proto__>[ Object ]|width=6|height=4"]; + ob_sqr [label = "<__proto__>[ Object ]|width=5"]; + + /* Function Objects */ + node [shape = Mrecord, fillcolor=orange, style=filled]; + fn_getArea [label="<__proto__>[getArea()]|\{return this.width * this.height\}"]; + fn_getPerimeter [label="<__proto__>[getPerimeter()]|\{return 2 * (this.width + this.height)\}"]; + fn_toString [label="<__proto__>[toString()]|\{return this.name + \" a=\" + …\}"]; + fn_getArea2 [label="<__proto__>[getArea()]|\{return this.width * this.width\}"]; + fn_getPerimeter2 [label="<__proto__>[getPerimeter()]|\{return 4 * this.width\}"]; + + /* References */ + top:Rectangle -> ob_Rectangle:__proto__; + top:Square -> ob_Square:__proto__; + top:rect -> ob_rect:__proto__; + top:sqr -> ob_sqr:__proto__; + ob_Rectangle:getArea -> fn_getArea:__proto__; + ob_Rectangle:getPerimeter -> fn_getPerimeter:__proto__; + ob_Rectangle:toString -> fn_toString:__proto__; + ob_Square:getArea -> fn_getArea2:__proto__; + ob_Square:getPerimeter -> fn_getPerimeter2:__proto__; + + /* Inheritance Chains */ + edge [style=dashed] + ob_rect:__proto__ -> ob_Rectangle:__proto__; + ob_sqr:__proto__ -> ob_Square:__proto__; + ob_Square:__proto__ -> ob_Rectangle:__proto__; + + +} \ No newline at end of file diff --git a/articles/object-graphs-2/prototypal.js b/articles/object-graphs-2/prototypal.js new file mode 100644 index 0000000..42124ce --- /dev/null +++ b/articles/object-graphs-2/prototypal.js @@ -0,0 +1,32 @@ +//rectangle +var Rectangle = { + name: "Rectangle", + getArea: function getArea() { + return this.width * this.height; + }, + getPerimeter: function getPerimeter() { + return 2 * (this.width + this.height); + }, + toString: function toString() { + return this.name + " a=" + this.getArea() + " p=" + this.getPerimeter(); + } +}; +//square +var Square = { + name: "Square", + getArea: function getArea() { + return this.width * this.width; + }, + getPerimeter: function getPerimeter() { + return this.width * 4; + }, +}; +Square.__proto__ = Rectangle; +//test +var rect = Object.create(Rectangle); +rect.width = 6; +rect.height = 4; +var square = Object.create(Square); +square.width = 5; +console.log(rect.toString()); +console.log(square.toString());