How do you check if a JavaScript Object is a DOM Object?

后端 未结 30 2798
-上瘾入骨i
-上瘾入骨i 2020-11-22 16:06

I\'m trying to get:

document.createElement(\'div\')  //=> true
{tagName: \'foobar something\'}  //=> false

In my own scripts, I used

30条回答
  •  再見小時候
    2020-11-22 16:23

    var IsPlainObject = function ( obj ) { return obj instanceof Object && ! ( obj instanceof Function || obj.toString( ) !== '[object Object]' || obj.constructor.name !== 'Object' ); },
        IsDOMObject = function ( obj ) { return obj instanceof EventTarget; },
        IsDOMElement = function ( obj ) { return obj instanceof Node; },
        IsListObject = function ( obj ) { return obj instanceof Array || obj instanceof NodeList; },
    

    // In fact I am more likely t use these inline, but sometimes it is good to have these shortcuts for setup code

提交回复
热议问题