Using Object.prototype.toString.call() to return o

2019-03-30 12:16发布

Hopefully I can ask this in an understandable way...

Overall, I am trying to determine what type of object I am currently dealing with.

I'm creating a collection (HTML is example, not literal) and I need to filter my collection to certain elements eg:

        <div id="tabContentWrap">
            <div id="tab">
                <a href="http://somelink">Link Element</a><img src="img.jpg" alt="img" />
                <select id="my_select"><option value="1">1</option></select>
            </div>
        </div>

function getFilteredElements() {
    var tabContent = getElementsByClass("tabContentWrap", document.getElementById(tabWrapId), "div");

    for (var j = 0; j < tabContent.length; j++){
        tabContentLinks = tabContent[j].getElementsByTagName('*');
        for (var k = 0; k < tabContentLinks.length; k++){
            // Here i attempt to filter the collection
            if (tabContentLinks[k] == '[object HTMLSelectElement]') {
                alert("found select list");
            }
         }
     }
 }

Which works fine in Mozilla but not in Internet Explorer 8, tabContentLinks[k] returns [object] instead of [object 'ObjectType']

So I investigated and discovered that you can use Object.prototype.toString.call(object) to get the object type, which again works fine in Mozilla but returns [object Object] in IE8...

I call

get_type(tabContentsLink[k]);

which runs the following function:

function get_type(thing){
    if (thing === null) return "[object Null]";
    // special case
    return Object.prototype.toString.call(thing);
}

But this just returns [object Object]

Does Object.prototype.toString.call() ever return the type of object in IE or am I very far off and barking up a lamppost instead of a tree?

4条回答
ゆ 、 Hurt°
2楼-- · 2019-03-30 12:22

I believe IE will return simple data types, but everything else is just an object. It's a browser limitation. I don't know if IE9 is improving the situation, but that probably wouldn't help you anyway.

查看更多
乱世女痞
3楼-- · 2019-03-30 12:27

I don't have IE handy, but the proper way in Firefox is to use object.constructor.name (object.constructor being the object's constructor, .name being the function name).

function Xyz() {}
new Xyz().constructor.name === 'Xyz'

But then, also,

var Abc = function() {}
new Abc().constructor.name === 'Abc'
查看更多
4楼-- · 2019-03-30 12:32

Well, first of all I want to tell you that Object.prototype.toString returns the value of the object's internal [[Class]] property, it isn't really a Type.

The value of this internal property represents the specification defined classification of an object (more info here).

Javascript has only 6 language types: Object, String, Number, Boolean, Null and Undefined, that's it.

The value of the [[Class]] internal property for host objects -as DOM elements- can be anything, it is completely implementation-dependent, on built-in objects is safe to use it -except with some exceptions in IE as @Alex pointed out in the article linked in his answer-.

You are working with DOM elements, and you want to figure out what kind of node it is, my suggestion to this, is to simply use the nodeName property (please avoid using tagName).

The nodeName property contains the name of the node you are dealing it, in upper case, therefore you could use it as this:

function getFilteredElements() {
  var tabContent = getElementsByClass("tabContentWrap", 
  document.getElementById(tabWrapId), "div");

  for (var j = 0; j < tabContent.length; j++){
    tabContentLinks = tabContent[j].getElementsByTagName('*');
    for (var k = 0; k < tabContentLinks.length; k++){
      // Here i attempt to filter the collection
      if (tabContentLinks[k].nodeName == 'SELECT') { // <- SELECT elements
        alert("found select list");
      }
    }
  }
}
查看更多
做个烂人
5楼-- · 2019-03-30 12:32

Rather than recreate the entire discussion and possible solutions, I'll just point you to a blog post that discusses this exact issue.

查看更多
登录 后发表回答