I'm trying to get:(我试图得到:)
document.createElement('div') //=> true
{tagName: 'foobar something'} //=> false
In my own scripts, I used to just use this since I never needed tagName
as a property:(在我自己的脚本中,我以前只是使用它,因为我从来不需要tagName
作为属性:)
if (!object.tagName) throw ...;
So for the second object, I came up with the following as a quick solution -- which mostly works.(因此,对于第二个对象,我想出了以下方法作为一种快速解决方案-大部分都能奏效。) ;)(;))
The problem is, it depends on browsers enforcing read-only properties, which not all do.(问题是,这取决于浏览器是否强制执行只读属性,而并非所有人都这样做。)
function isDOM(obj) {
var tag = obj.tagName;
try {
obj.tagName = ''; // Read-only for DOM, should throw exception
obj.tagName = tag; // Restore for normal objects
return false;
} catch (e) {
return true;
}
}
Is there a good substitute?(有没有好的替代品?)
ask by Jonathan Lonowski translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…