Loading ...
Sorry, an error occurred while loading the content.

1160Re: I must use 'new String'

Expand Messages
  • Woomla
    Feb 18, 2010
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, Stefan Weiss <weiss@...> wrote:
      > The OP was planning on using a String object, which would have a type of
      > "object", so I don't think that matters much. Furthermore, the receiving
      > code wasn't expecting normal Strings anyway (it would be looking for an
      > "id" property). If plain Object objects are too "weird", he could make
      > it official and define a constructor:
      >
      > function SpecialString (str, id) {
      > this.value = str;
      > this.id = id;
      > }
      >
      > var str = new SpecialString("somestring", 123);
      >
      > This way, there would be no confusion, and no more JSLint warnings.


      I've come up with the solution below. It runs in FF and IE and it uses data hiding. By implementing toString it behaves like a regular string. Only typeof returns 'object'.


      var MyString = (function ()
      {
      var thevalue, itself;
      thevalue = '';
      itself = function (value)
      {
      thevalue = value;
      };
      itself.prototype.$family = {name: 'mystring'};
      itself.prototype.toString = function ()
      {
      return thevalue;
      };
      return itself;
      }());
    • Show all 15 messages in this topic