Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constructor.name is undefined in Internet Explorer

My debugging work in IE ended today by finding that constructor.name is undefined.

I created the following simple code that reproduces the issue:

({}).constructor.name === undefined // => true

Is there any workaround to make this work?

Maybe overriding somehow the prototype?

If possible, I don't want to change the syntax, because the change would be major.

JSFIDDLE

like image 445
Ionică Bizău Avatar asked Aug 05 '14 14:08

Ionică Bizău


People also ask

Does Internet Explorer support JavaScript classes?

Save this question. Show activity on this post. I wrote some Javascript code that works perfectly in all major browsers.

How to create constructor function in JavaScript?

In JavaScript, multiple objects can be created in a constructor: //Constructor function User() { this.name = 'Bob'; } var user1 = new User(); var user2 = new User(); In the above example, two objects are created using the same constructor.

How to Configure an object using a constructor?

Assigning the constructor property to an object One can assign the constructor property of non-primitives. const arr = []; arr. constructor = String; arr. constructor === String; // true arr instanceof String; // false arr instanceof Array; // true const foo = new Foo(); foo.

What is object constructor in JavaScript?

Object() constructor The Object constructor turns the input into an object. Its behavior depends on the input's type. If the value is null or undefined , it creates and returns an empty object. Otherwise, it returns an object of a Type that corresponds to the given value.


2 Answers

From matt.scharley.me

/**
 * Hack in support for Function.name for browsers that don't support it.
 * IE, I'm looking at you.
**/
if (Function.prototype.name === undefined && Object.defineProperty !== undefined) {
    Object.defineProperty(Function.prototype, 'name', {
        get: function() {
            var funcNameRegex = /function\s([^(]{1,})\(/;
            var results = (funcNameRegex).exec((this).toString());
            return (results && results.length > 1) ? results[1].trim() : "";
        },
        set: function(value) {}
    });
}
like image 180
Olivier Avatar answered Sep 20 '22 19:09

Olivier


The problem is simply that the name property of function objects is not supported in Internet Explorer. The property is non-standard (up until ECMAScript 6, at least) so it's not altogether surprising.

There isn't a completely reliable workaround so I would suggest trying to do without it if possible. However, you may be able to extract the name from the string representation of the function. Here a couple of links that deal with this that I got from a quick search:

  • Javascript get Function Name?
  • https://gist.github.com/dfkaye/6384439

Update

From the comments, it turns out that the goal of the question author is to test whether a variable is a reference to a plain object create by the Object constructor. A reliable way of doing this for a variable a is

Object.prototype.toString.call(a) == "[object Object]"

For more information I recommend the following page written by Angus Croll:

http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/

like image 40
Tim Down Avatar answered Sep 17 '22 19:09

Tim Down