From bdd66ab21a321e49e7c06b5600eeed1dd468d561 Mon Sep 17 00:00:00 2001 From: Mikael Karon Date: Fri, 22 Feb 2013 14:34:20 +0800 Subject: [PATCH] Add toString to Constructor. Closes troopjs/troopjs-core#83 --- src/component/factory.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/component/factory.js b/src/component/factory.js index 940e4eb..87fb353 100644 --- a/src/component/factory.js +++ b/src/component/factory.js @@ -6,11 +6,14 @@ define([ "troopjs-utils/unique", "poly/object" ], function ComponentFactoryModule(unique) { /*jshint laxbreak:true */ - var ARRAY_PROTO = Array.prototype; + var PROTOTYPE = "prototype"; + var TOSTRING = "toString"; + var ARRAY_PROTO = Array[PROTOTYPE]; var ARRAY_PUSH = ARRAY_PROTO.push; var ARRAY_UNSHIFT = ARRAY_PROTO.unshift; + var OBJECT_TOSTRING = Object[PROTOTYPE][TOSTRING]; var TYPEOF_FUNCTION = typeof function () {}; - var PROTOTYPE = "prototype"; + var DISPLAYNAME = "displayName"; var LENGTH = "length"; var EXTEND = "extend"; var CREATE = "create"; @@ -151,6 +154,19 @@ define([ "troopjs-utils/unique", "poly/object" ], function ComponentFactoryModul return descriptor; }; + /** + * Returns a string representation of this constructor + * @returns {String} + */ + function ConstructorToString() { + var self = this; + var prototype = self[PROTOTYPE]; + + return DISPLAYNAME in prototype + ? prototype[DISPLAYNAME] + : OBJECT_TOSTRING.call(self); + } + /** * Creates components * @returns {*} New component @@ -328,6 +344,10 @@ define([ "troopjs-utils/unique", "poly/object" ], function ComponentFactoryModul "value" : specials }; + constructorDescriptors[TOSTRING] = { + "value" : ConstructorToString + }; + // Add EXTEND to constructorDescriptors constructorDescriptors[EXTEND] = { "value" : extend