Firefox 17在使用“typeof”时报告创建的DOM元素(HTMLObjectElement)作为函数。 [英] Firefox 17 reports created DOM element (HTMLObjectElement) as function when using "typeof"
问题描述
此对象被保存/存储在一个名为o.data.cam的变量中。其他功能检查此变量是否有效。
例如:
if(typeof o.data.cam ==object)
{do this}
在所有浏览器中都可以正常工作EXCEPT firefox。 o.data.cam
是一个函数,而不是一个对象。
这是一个奇怪的行为,我认为,因为对象被创建,为什么这被报告为功能?
在Firefox 17.0.1(最新) HTMLObjectElement(object tag)在转储时返回[object HTMLObjectElement]。
我可以理解这个(?),因为它可以和本机函数(protected)相同,例如像native function => [native function]。否则有点奇怪,因为大多数DOM元素可以被检查,所以为什么不是他的?
但与其他浏览器进行比较时,有一些区别:
Firefox Chrome MSIE Opera
___________________________________________________________________________________________
dump o.data.cam [object HTMLObjectElement] Object * Object * Object *
typeof o.data.camfunctionObjectObjectObject
typeof HTMLObjectElementobjectfunctionObjectfunction
$ c $ (asterisk)=打印对象树
$>
$ b
注意:
- 引号之间是一个字符串,typeof结果。
所有浏览器中的 (Firefox除外),这在我看来是正确的,
,因为它是一个功能和创建为类(新功能)它必须作为对象报告(我想?)
我的问题是:
这是一个错误,或者我错过了一些?
编辑:创建此函数来检查它是否是有效的DOM元素。这样做是安全的吗?现在Firefox返回true
function isDOM(oo)
{
if(oo )
{
if(typeof oo instanceof jQuery)
{return !! oo [0]; }
if(typeof oo =='object'|| typeof oo =='function')
{return(typeof oo.tagName =='string'&&oo.tagName.length> ; 0); }
}
return false;
}
解决方案根据EcmaScript规范,所有具有[[Call]]内部方法的对象必须为typeof返回function。
HTMLObjectElement的实例可以具有[[Call]]内部方法, ins可以使对象被实例化为可调用。
所以唯一的问题是对象是否应该永远是可调用的,如果插件不想要的话被调用或者是否应该根据插件在做什么动态地改变对象和功能之间的类型。还有第三个选项,即使有[[Call]]也是忽略规范和报告对象,我猜。
Firefox是前者:总是有[[呼叫]],所以类型报告功能。其他浏览器可能会使[[呼叫]]出现并动态消失或违反规范。
I have a flash object (HTMLObjectElement) created (with jQuery) and added it to the DOM. That is working OK in all browsers.
This object is saved/stored in a variable called "o.data.cam". Other functions checking this variable if it is valid.
For example:
if( typeof o.data.cam == "object")
{ do this }
This is working OK in all browsers EXCEPT firefox. o.data.cam
is a function instead of an object.
That is a strange behaviour i think, because the object is created, why is this reported as function?
In Firefox 17.0.1 (latest) a created HTMLObjectElement (object tag) returns [object HTMLObjectElement] when dump it.
I can understand this (?), because it can be same as a native function (protected), for example like a native function => [native function]. Otherwise a bit strange because most of the DOM-elements can be inspected so why not his one?
But there are some differences to notice when comparing to other browsers:
Firefox Chrome MSIE Opera
___________________________________________________________________________________________
dump o.data.cam [object HTMLObjectElement] Object* Object* Object*
typeof o.data.cam "function" "Object" "Object" "Object"
typeof HTMLObjectElement "object" "function" "Object" "function"
- (asterisk) = prints Object tree
NOTES:
- Between quotes is a string, a "typeof" result.
The typeof evaluation returns in all browsers "Object" (except Firefox), which is in my opinion correct,
because when it is a function and created as class (new function) it must be reported as object (i think?)
My question is:
Is this a bug or do i missing something?
EDIT: Created this function to check if it is a valid DOM element. Is it safe to do it this way? Now Firefox returns true.
function isDOM(oo)
{
if( oo )
{
if( typeof oo instanceof jQuery )
{ return !!oo[0]; }
if( typeof oo == 'object' || typeof oo == 'function' )
{ return ( typeof oo.tagName == 'string' && oo.tagName.length > 0 ); }
}
return false;
}
解决方案 Per the EcmaScript spec, all objects with a [[Call]] internal method must return "function" for typeof.
Instances of HTMLObjectElement can have a [[Call]] internal method, because plug-ins can make the object they're instantiated for callable.
So the only question is whether the object should always be callable and throw if the plug-in doesn't want to be called or whether it should dynamically change type between "object" and "function" depending on what the plug-in is doing. There's also the third option of just ignoring the spec and reporting "object" even though there is a [[Call]], I guess.
Firefox does the former: always have a [[Call]], so typeof reports "function". The other browsers are presumably either making [[Call]] appear and disappear dynamically or flat out violating the spec.
这篇关于Firefox 17在使用“typeof”时报告创建的DOM元素(HTMLObjectElement)作为函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!