Get the name of an object's type

前端 未结 20 2249
忘掉有多难
忘掉有多难 2020-11-21 22:37

Is there a JavaScript equivalent of Java\'s class.getName()?

相关标签:
20条回答
  • 2020-11-21 22:48

    You can use the instanceof operator to see if an object is an instance of another, but since there are no classes, you can't get a class name.

    0 讨论(0)
  • 2020-11-21 22:52

    Jason Bunting's answer gave me enough of a clue to find what I needed:

    <<Object instance>>.constructor.name
    

    So, for example, in the following piece of code:

    function MyObject() {}
    var myInstance = new MyObject();
    

    myInstance.constructor.name would return "MyObject".

    0 讨论(0)
  • 2020-11-21 22:53

    Using Object.prototype.toString

    It turns out, as this post details, you can use Object.prototype.toString - the low level and generic implementation of toString - to get the type for all built-in types

    Object.prototype.toString.call('abc') // [object String]
    Object.prototype.toString.call(/abc/) // [object RegExp]
    Object.prototype.toString.call([1,2,3]) // [object Array]
    

    One could write a short helper function such as

    function type(obj){
        return Object.prototype.toString.call(obj]).match(/\s\w+/)[0].trim()
    }
    
    return [object String] as String
    return [object Number] as Number
    return [object Object] as Object
    return [object Undefined] as Undefined
    return [object Function] as Function
    
    0 讨论(0)
  • 2020-11-21 22:53

    Say you have var obj;

    If you just want the name of obj's type, like "Object", "Array", or "String", you can use this:

    Object.prototype.toString.call(obj).split(' ')[1].replace(']', '');
    
    0 讨论(0)
  • 2020-11-21 22:54

    You should use somevar.constructor.name like a:

        
        const getVariableType = a => a.constructor.name.toLowerCase();
    
        const d = new Date();
        const res1 = getVariableType(d); // 'date'
        const num = 5;
        const res2 = getVariableType(num); // 'number'
        const fn = () => {};
        const res3 = getVariableType(fn); // 'function'
    
        console.log(res1); // 'date'
        console.log(res2); // 'number'
        console.log(res3); // 'function'

    0 讨论(0)
  • 2020-11-21 22:55

    I was actually looking for a similar thing and came across this question. Here is how I get types: jsfiddle

    var TypeOf = function ( thing ) {
    
        var typeOfThing = typeof thing;
    
        if ( 'object' === typeOfThing ) {
    
            typeOfThing = Object.prototype.toString.call( thing );
    
            if ( '[object Object]' === typeOfThing ) {
    
                if ( thing.constructor.name ) {
                    return thing.constructor.name;
                } 
    
                else if ( '[' === thing.constructor.toString().charAt(0) ) {
                    typeOfThing = typeOfThing.substring( 8,typeOfThing.length - 1 );
                } 
    
                else {
    
                    typeOfThing = thing.constructor.toString().match( /function\s*(\w+)/ );
    
                    if ( typeOfThing ) { 
                        return typeOfThing[1];
                    } 
    
                    else {
                        return 'Function';
                    }
                }
            } 
    
            else {
                typeOfThing = typeOfThing.substring( 8,typeOfThing.length - 1 );
            }
        }
    
        return typeOfThing.charAt(0).toUpperCase() + typeOfThing.slice(1);
    }
    
    0 讨论(0)
提交回复
热议问题