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

Apparently neither instanceof

nor is typeof

it supplied in terms of correctly identifying the type of each javascript object. I came 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!

+2


a source to share


2 answers


In some cases, relying on the operator is instanceof

not good enough .

A known issue is that wonk does not work in multi-frame environments.

The operator is typeof

not that useful and there are some implementation bugs, for example in Chrome or Firefox 2.x, where RegExp

objects show up as "function"

because they were made callable (for example /foo/(str);

).

A property constructor

can be changed and you should never trust it.

Finally, the method Function.prototype.toString

is implementation dependent, which means that the implementation may not even include the function name in the string representation of the function ...

A few days ago I created a simple yet robust type discovery function, it uses typeof

for primitive values ​​and relies on an [[Class]]

intrinsic property for objects.

All objects have this property, implementations use it internally to discover the type of the object, it is completely immutable and only accessible via Object.prototype.toString

:

Using:

//...
if (typeString(obj) == 'array') {
  //..
}

      



Implementation:

function typeString(o) {
  if (typeof o != 'object')
    return typeof o;

  if (o === null)
      return "null";
  //object, array, function, date, regexp, string, number, boolean, error
  var internalClass = Object.prototype.toString.call(o)
                                               .match(/\[object\s(\w+)\]/)[1];
  return internalClass.toLowerCase();
}

      

the second version of this function is stricter because it only returns the built-in object types described in the ECMAScript specification.

Possible output values:

Primitives:

  • "number"

  • "string"

  • "boolean"

  • "undefined"

  • "null"

  • "object"

Built-in object types (via [[Class]]

)

  • "function"

  • "array"

  • "date"

  • "regexp"

  • "error"

+3


a source


A similar question arose a few days ago. I hacked open jQuery 1.4.2 to see how it was done. Here are my results so far, you can run checks on the rest of them. Im sure:

(function() {

    // Define the base sys namespace
    this.sys = function() { };

    var toString = Object.prototype.toString;

    //from jQuery 1.4.2    
    sys.isFunction = function(obj) {
        return toString.call(obj) === "[object Function]";
    }

    //from jQuery 1.4.2
    sys.isArray = function(obj) {
        return toString.call(obj) === "[object Array]";
    }
}

      



Using:

if (sys.isArray(myObject)) doStuff();

      

+1


a source







All Articles