Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Update as @lonesomeday@lonesomeday mentioned in the comments, it seems that I'm trying to get the name of an anonymous function. That looks a little bit scary to me. Is that even possible?

Update as @jackson@jackson said below, it seems that the correct output should be function not me. Is that really correct?

Update as @lonesomeday mentioned in the comments, it seems that I'm trying to get the name of an anonymous function. That looks a little bit scary to me. Is that even possible?

Update as @jackson said below, it seems that the correct output should be function not me. Is that really correct?

Update as @lonesomeday mentioned in the comments, it seems that I'm trying to get the name of an anonymous function. That looks a little bit scary to me. Is that even possible?

Update as @jackson said below, it seems that the correct output should be function not me. Is that really correct?

added 146 characters in body
Source Link
Mahdi
  • 9.4k
  • 9
  • 58
  • 75

Update here is the final and working code -- just in case if somebody found it helpful: jsfiddle

I've written this as the type detector. It works very well in all cases -- so far whatever I've tested, but fails only in one case.

I've written this as the type detector. It works very well in all cases -- so far whatever I've tested, but fails only in one case.

Update here is the final and working code -- just in case if somebody found it helpful: jsfiddle

I've written this as the type detector. It works very well in all cases -- so far whatever I've tested, but fails only in one case.

added 186 characters in body
Source Link
Mahdi
  • 9.4k
  • 9
  • 58
  • 75

I've written this as the type detector. It works very well in all cases -- so far whatever I've tested, but fails only in one case.

First, here is the snippet: jsfiddle

var TypeOf = function ( thing ) {
    var typeOfThing = typeof thing;
    if ( typeOfThing === 'object' ) {
        typeOfThing = Object.prototype.toString.call(thing);
        if ( typeOfThing === '[object Object]') {
            if ( thing.constructor.name ) 
                typeOfThing = 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+)/)[1];
        } else {
            typeOfThing = typeOfThing.substring(8,typeOfThing.length - 1);
        }
        return typeOfThing.toLowerCase();
    } else {
        return typeOfThing;
    }
}

The problem is that if I define the function at parse-time then it will work perfectly fine:

function me () {};
var you = new me();
console.log('Type of you: ' + TypeOf(you));    // me

However if I just define it at run-time, then it won't work:

var me = function () {};
var you = new me();
console.log('Type of you: ' + TypeOf(you));    // nope

As far as I can see in the parse-time case the constructor is something like function me () {}, so I can get it with /function\s*(\w+)/, however in the run-time case the constructor is function () {}.

Is there any way to get this to work? And also, are there any other cases that this snippet might potentially fail to detect the type?

Update as @lonesomeday mentioned in the comments, it seems that I'm trying to get the name of an anonymous function. That looks a little bit scary to me. Is that even possible?

Update as @jackson said below, it seems that the correct output should be function not me. Is that really correct?

I've written this as the type detector. It works very well in all cases -- so far whatever I've tested, but fails only in one case.

First, here is the snippet: jsfiddle

var TypeOf = function ( thing ) {
    var typeOfThing = typeof thing;
    if ( typeOfThing === 'object' ) {
        typeOfThing = Object.prototype.toString.call(thing);
        if ( typeOfThing === '[object Object]') {
            if ( thing.constructor.name ) 
                typeOfThing = 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+)/)[1];
        } else {
            typeOfThing = typeOfThing.substring(8,typeOfThing.length - 1);
        }
        return typeOfThing.toLowerCase();
    } else {
        return typeOfThing;
    }
}

The problem is that if I define the function at parse-time then it will work perfectly fine:

function me () {};
var you = new me();
console.log('Type of you: ' + TypeOf(you));    // me

However if I just define it at run-time, then it won't work:

var me = function () {};
var you = new me();
console.log('Type of you: ' + TypeOf(you));    // nope

As far as I can see in the parse-time case the constructor is something like function me () {}, so I can get it with /function\s*(\w+)/, however in the run-time case the constructor is function () {}.

Is there any way to get this to work? And also, are there any other cases that this snippet might potentially fail to detect the type?

Update as @lonesomeday mentioned in the comments, it seems that I'm trying to get the name of an anonymous function. That looks a little bit scary to me. Is that even possible?

I've written this as the type detector. It works very well in all cases -- so far whatever I've tested, but fails only in one case.

First, here is the snippet: jsfiddle

var TypeOf = function ( thing ) {
    var typeOfThing = typeof thing;
    if ( typeOfThing === 'object' ) {
        typeOfThing = Object.prototype.toString.call(thing);
        if ( typeOfThing === '[object Object]') {
            if ( thing.constructor.name ) 
                typeOfThing = 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+)/)[1];
        } else {
            typeOfThing = typeOfThing.substring(8,typeOfThing.length - 1);
        }
        return typeOfThing.toLowerCase();
    } else {
        return typeOfThing;
    }
}

The problem is that if I define the function at parse-time then it will work perfectly fine:

function me () {};
var you = new me();
console.log('Type of you: ' + TypeOf(you));    // me

However if I just define it at run-time, then it won't work:

var me = function () {};
var you = new me();
console.log('Type of you: ' + TypeOf(you));    // nope

As far as I can see in the parse-time case the constructor is something like function me () {}, so I can get it with /function\s*(\w+)/, however in the run-time case the constructor is function () {}.

Is there any way to get this to work? And also, are there any other cases that this snippet might potentially fail to detect the type?

Update as @lonesomeday mentioned in the comments, it seems that I'm trying to get the name of an anonymous function. That looks a little bit scary to me. Is that even possible?

Update as @jackson said below, it seems that the correct output should be function not me. Is that really correct?

added 247 characters in body
Source Link
Mahdi
  • 9.4k
  • 9
  • 58
  • 75
Loading
Source Link
Mahdi
  • 9.4k
  • 9
  • 58
  • 75
Loading