Is this a good way to identify the type of a javascript object?

Posted by FK82 on Stack Overflow See other posts from Stack Overflow or by FK82
Published on 2010-05-05T18:24:30Z Indexed on 2010/05/05 18:28 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

Apparently neither instanceof nor typeof deliver in terms of correctly identifying the type of every javascript object. I have come up with this function and I'm looking for some feedback:

    function getType() {

        var input = arguments[0] ;

        var types = ["String","Array","Object","Function","HTML"] ; //!! of the top of my head

        for(var n=0; n < types.length; n++) {

            if( input.constructor.toString().indexOf( types[n] ) != -1) {
                document.write( types[n] ) ;
            }

        }

    }

Thanks for reading!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about identifying