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

1161Re: [jslint] Re: I must use 'new String'

Expand Messages
  • Klemen Slavič
    Feb 18, 2010
    • 0 Attachment
      Regarding the type; I think it's for the best that it returns 'object'
      instead of 'string'; that way, you know you're handling a special type
      instead of a vanilla string, which is much more concise and understandable.



      On 18 February 2010 15:13, Woomla <woomla@...> wrote:

      >
      >
      > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.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;
      > }());
      >
      >
      >


      [Non-text portions of this message have been removed]
    • Show all 15 messages in this topic