From d74bf1e39a64650ba9965604ab9665113d8977b9 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 11 Jan 2013 15:17:21 -0800 Subject: [PATCH] Use proper prototypal inheritance and less dynamic new --- js/id/connection.js | 11 ++--------- js/id/graph/entity.js | 13 ------------- js/id/graph/node.js | 12 +++++++++++- js/id/graph/relation.js | 12 +++++++++++- js/id/graph/way.js | 12 +++++++++++- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/js/id/connection.js b/js/id/connection.js index 003750ff2..7b9f98a27 100644 --- a/js/id/connection.js +++ b/js/id/connection.js @@ -77,15 +77,8 @@ iD.Connection = function() { delete o.lat; } o.id = iD.Entity.id.fromOSM(o.type, o.id); - switch (o.type) { - case 'node': - o._poi = !refNodes[o.id]; - return iD.Node(o); - case 'way': - return iD.Way(o); - case 'relation': - return iD.Relation(o); - } + if (o.type === 'node') o._poi = !refNodes[o.id]; + return new iD.Entity[o.type](o); } function parse(dom) { diff --git a/js/id/graph/entity.js b/js/id/graph/entity.js index 8c8f9a6fb..45d0d0c5c 100644 --- a/js/id/graph/entity.js +++ b/js/id/graph/entity.js @@ -109,16 +109,3 @@ iD.Entity.prototype = { return n.length === 0 ? 'unknown' : n.join('; '); } }; - -iD.Entity.extend = function(properties) { - var Subclass = function() { - if (this instanceof Subclass) return; - return (new Subclass()).initialize(arguments); - }; - - Subclass.prototype = new iD.Entity(); - _.extend(Subclass.prototype, properties); - iD.Entity[properties.type] = Subclass; - - return Subclass; -}; diff --git a/js/id/graph/node.js b/js/id/graph/node.js index fd3a1cd69..d72bf6442 100644 --- a/js/id/graph/node.js +++ b/js/id/graph/node.js @@ -1,4 +1,14 @@ -iD.Node = iD.Entity.extend({ +iD.Node = iD.Entity.node = function iD_Node() { + if (!(this instanceof iD_Node)) { + return (new iD_Node()).initialize(arguments); + } else if (arguments.length) { + this.initialize(arguments); + } +}; + +iD.Node.prototype = Object.create(iD.Entity.prototype); + +_.extend(iD.Node.prototype, { type: "node", extent: function() { diff --git a/js/id/graph/relation.js b/js/id/graph/relation.js index dc298ecbd..e7d4afd0a 100644 --- a/js/id/graph/relation.js +++ b/js/id/graph/relation.js @@ -1,4 +1,14 @@ -iD.Relation = iD.Entity.extend({ +iD.Relation = iD.Entity.relation = function iD_Relation() { + if (!(this instanceof iD_Relation)) { + return (new iD_Relation()).initialize(arguments); + } else if (arguments.length) { + this.initialize(arguments); + } +}; + +iD.Relation.prototype = Object.create(iD.Entity.prototype); + +_.extend(iD.Relation.prototype, { type: "relation", members: [], diff --git a/js/id/graph/way.js b/js/id/graph/way.js index 9efd4fe6c..35834e761 100644 --- a/js/id/graph/way.js +++ b/js/id/graph/way.js @@ -1,4 +1,14 @@ -iD.Way = iD.Entity.extend({ +iD.Way = iD.Entity.way = function iD_Way() { + if (!(this instanceof iD_Way)) { + return (new iD_Way()).initialize(arguments); + } else if (arguments.length) { + this.initialize(arguments); + } +}; + +iD.Way.prototype = Object.create(iD.Entity.prototype); + +_.extend(iD.Way.prototype, { type: "way", nodes: [],