Use instanceof trick for consistent construction style

Fixes #67.
This commit is contained in:
John Firebaugh
2012-11-09 16:33:47 -08:00
parent ff836560d0
commit 7f2dc815cd
6 changed files with 17 additions and 8 deletions

View File

@@ -8,6 +8,13 @@ Most of iD is written with [a js module pattern](http://macwright.org/2012/06/04
that is, they do not use the `new` operator, and they use scope instead of
`this` to reference variables and functions.
The exceptions are classes that are very heavily instantiated: Graphs and entities. These are written in a
classical manner for [performance reasons](https://gist.github.com/3961693).
In order to unify the construction interface for these two styles, classical classes use the
[instanceof trick](http://ejohn.org/blog/simple-class-instantiation/). This allows instantiation
of both module pattern classes and classical classes to be done without using `new`.
Function names
--------------
Anything that creates and calls an Action should be prefixed with do:

View File

@@ -47,7 +47,7 @@
<script type='text/javascript' src='js/iD/actions/actions.js'></script>
<script type='text/javascript' src='js/iD/actions/operations.js'></script>
<script type='text/javascript' src='js/iD/controller/controller.js'></script>
<script type='text/javascript' src='js/iD/controller/Controller.js'></script>
<script type='text/javascript' src='js/iD/format/format.js'></script>
<script type='text/javascript' src='js/iD/format/GeoJSON.js'></script>
@@ -74,7 +74,7 @@
iD.Hash().map(map);
iD.UI.bind();
var controller = iD.controller(map);
var controller = iD.Controller(map);
d3.selectAll('button#place').on('click', function() {
controller.go(iD.actions.AddPlace);

View File

@@ -1,6 +1,6 @@
// A controller holds a single action at a time and calls `.enter` and `.exit`
// to bind and unbind actions.
iD.controller = function(map) {
iD.Controller = function(map) {
var controller = { action: null };
controller.go = function(x) {

View File

@@ -1,4 +1,5 @@
iD.Graph = function(entities, annotation) {
if (!(this instanceof iD.Graph)) return new iD.Graph(entities, annotation)
this.entities = entities || {};
this.annotation = annotation;
};
@@ -36,11 +37,11 @@ iD.Graph.prototype = {
replace: function(entity, annotation) {
var o = {};
o[entity.id] = entity;
return new iD.Graph(pdata.object(this.entities).set(o).get(), annotation);
return iD.Graph(pdata.object(this.entities).set(o).get(), annotation);
},
remove: function(entity, annotation) {
return new iD.Graph(pdata.object(this.entities).remove(entity.id).get(), annotation);
return iD.Graph(pdata.object(this.entities).remove(entity.id).get(), annotation);
},
// get all objects that intersect an extent.

View File

@@ -1,5 +1,6 @@
iD.History = function() {
this.stack = [new iD.Graph()];
if (!(this instanceof iD.History)) return new iD.History();
this.stack = [iD.Graph()];
this.index = 0;
};

View File

@@ -22,8 +22,8 @@ iD.Map = function(elem) {
width, height,
dispatch = d3.dispatch('move', 'update'),
// data
history = new iD.History(),
connection = new iD.Connection(history.graph()),
history = iD.History(),
connection = iD.Connection(history.graph()),
inspector = iD.Inspector(history),
parent = d3.select(elem),
selection = [],