checking type?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    checking type?

    Howdy All!

    How can you tell the type of an object?

    I am not talking about using "typeof" ..

    I am talking about being able to determine if an object is a Date or String
    ....

    Thanks for any advice!

    Rob
    :)


  • Janwillem Borleffs

    #2
    Re: checking type?


    "Robert Mark Bram" <robert.bram@yo urshoesinfotech .monash.edu.au> schreef in
    bericht news:3f5f12cc$0 $4187$afc38c87@ news.optusnet.c om.au...[color=blue]
    > Howdy All!
    >
    > How can you tell the type of an object?
    >
    > I am not talking about using "typeof" ..
    >
    > I am talking about being able to determine if an object is a Date or[/color]
    String[color=blue]
    > ...
    >[/color]

    You could test for methods:

    function test (obj) {
    var type = typeof obj;
    if (type == 'object') {
    if (obj.getDate) return 'Date';
    if (obj.split) return 'String';
    return object;
    }
    return type;
    }

    alert(test('tes t')); // Alerts 'string'
    alert(test(new String('test')) ); // Alerts 'String';
    alert(test(new Date())); // Alerts 'Date';


    JW



    Comment

    • Janwillem Borleffs

      #3
      Re: checking type?


      "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> schreef in bericht
      news:3f5f1b3a$0 $28895$1b62eedf @news.euronet.n l...[color=blue]
      >
      > function test (obj) {
      > var type = typeof obj;
      > if (type == 'object') {
      > if (obj.getDate) return 'Date';
      > if (obj.split) return 'String';
      > return object;
      > }
      > return type;
      > }
      >[/color]

      Typo here, the statement:
      return object;

      should be:
      return 'object';

      JW



      Comment

      • Janwillem Borleffs

        #4
        Re: checking type?


        "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> schreef in bericht
        news:3f5f1bb2$0 $28889$1b62eedf @news.euronet.n l...[color=blue]
        >
        > "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> schreef in bericht
        > news:3f5f1b3a$0 $28895$1b62eedf @news.euronet.n l...[color=green]
        > >
        > > function test (obj) {
        > > var type = typeof obj;
        > > if (type == 'object') {
        > > if (obj.getDate) return 'Date';
        > > if (obj.split) return 'String';
        > > return object;
        > > }
        > > return type;
        > > }
        > >[/color]
        >[/color]

        And to make it more compact:

        function test (obj) {
        var type = typeof obj;
        if (type == 'object') {
        if (obj.getDate) return 'Date';
        if (obj.split) return 'String';
        }
        return type;
        }


        :-)

        JW



        Comment

        • Martin Honnen

          #5
          Re: checking type?



          Robert Mark Bram wrote:[color=blue]
          > Howdy All!
          >
          > How can you tell the type of an object?
          >
          > I am not talking about using "typeof" ..
          >
          > I am talking about being able to determine if an object is a Date or String
          > ...[/color]

          The strings you encounter in JavaScript are not usually objects at all
          but primitive string values. And
          typeof varName == 'string'
          tells you whether something is a string value.
          As for Date objects
          if (typeof varName == 'object' && varName.constru ctor == Date)

          --

          Martin Honnen


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: checking type?

            "Robert Mark Bram" <robert.bram@yo urshoesinfotech .monash.edu.au> writes:
            [color=blue]
            > How can you tell the type of an object?[/color]
            [color=blue]
            > I am not talking about using "typeof" ..[/color]

            Yes, that would just give "object".
            [color=blue]
            > I am talking about being able to determine if an object is a Date or String[/color]

            All built in constructors (String, Boolean, Number, Array, Date, RegExp, etc.)
            have a prototype with a property called "constructo r" that points to itself.

            That is
            var x = new String("foo");
            alert(x.constru ctor == String);
            alerts true. You can use this to recognize the objects constructed by these
            constructors *unless* someone fiddled with the constructor property. There
            is nothing that prevents you from doing:
            String.prototyp e.constructor = Array
            (except common sense). So, the method is not fool proof, if the fools are
            too inventive.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Richard Cornford

              #7
              Re: checking type?

              "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
              news:3f5f2312$0 $28891$1b62eedf @news.euronet.n l...
              <snip>[color=blue]
              > function test (obj) {
              > var type = typeof obj;
              > if (type == 'object') {
              > if (obj.getDate) return 'Date';
              > if (obj.split) return 'String';
              > }
              > return type;
              > }[/color]

              If - obj - happened to be - null - then - type - would still be "object"
              but the property testing would cause errors. Maybe:-

              if((obj)&&(type == 'object')){

              - would be safer.

              Richard.


              Comment

              • Douglas Crockford

                #8
                Re: checking type?

                > If - obj - happened to be - null - then - type - would still be "object"[color=blue]
                > but the property testing would cause errors. Maybe:-
                >
                > if ((obj) && (type == 'object')){[/color]

                Yup. I put stuff like that into so functions. For example,

                function isFunction(a) {
                return typeof a == 'function';
                }
                function isNull(a) {
                return typeof a == 'object' && !a;
                }
                function isNumber(a) {
                return typeof a == 'number' && isFinite(a);
                }
                function isObject(a) {
                return (typeof a == 'object' && a) || isFunction(a);
                }

                I find that typeof's own understanding of types is too fuzzy.



                Comment

                • Keith Bowes

                  #9
                  Re: checking type?

                  Robert Mark Bram wrote:[color=blue]
                  > Howdy All!
                  >
                  > How can you tell the type of an object?
                  >
                  > I am not talking about using "typeof" ..
                  >
                  > I am talking about being able to determine if an object is a Date or String
                  > ...
                  >[/color]

                  You can use the instanceof operator:
                  var now = new Date();
                  alert(now instanceof Date);


                  Comment

                  Working...