如何检查是否有拉斐尔物体? [英] How can I check if something is a Raphael object?
问题描述
给定一个JavaScript对象,如何检查它是否是Raphael对象(不是纸张,而是圆圈,路径等)?
Raphael.el
表示通用元素原型;我想我要测试
x .__ proto__ === Raphael.el
以交叉浏览器的方式,但我不完全确定。
稍微详细说明并添加一些更相关的信息(我花了一点时间才弄清楚接受的答案,我显然不是唯一一个看其他答案,而且,接受的答案只能起作用对于一个类型的Raphael对象:它解决了原始问题,这是一个更完整的资源。)
检测拉斐尔元素
使用 x.constructor.prototype == Raphael.el
,你是取 x
,该变量可能是Raphael 元素 (圈子,路径等 - 不 a Raphael set
或 paper
object)并将构建它的函数的原型与Raphael的原型进行比较拉斐尔本身的元素( Raphael是一个函数对象,el是它的一个定义属性。
这有效,但它也找不到基于不同原型的raphael对象到Raphael.el ,像套装和纸张对象:
检测Raphael套装
如果你想测试某些东西是拉斐尔集合,那么集合原型是在 Raphael.st
中,所以你可以测试变量是否是拉斐尔集合使用:
someSet.constructor.prototype == Raphael.st
检测Raphael纸张对象
至于嗅探Raphael 纸张对象的等价物,因为它们是使用 Raphael()
函数创建,您可以使用:
paper.constructor.prototype == Raphael.prototype
以上三者与......基本相同/ p>
someSet.constructor.prototype == paper.circle()。construc tor.prototype
...或...
someSet.constructor.prototype == paper.set()。constructor.prototype
...或...
someSet.constructor.prototype == Raphael()。constructor.prototype
...但是没有实际运行这些函数,所以避免浪费处理(并避免Raphael()抱怨它还没有传递ID)。
检测物体的子类型(例如矩形,圆...)
以上都不适用于Raphael元素的子类型 - 例如如果你将一个圆与 R.rect()。constructor.prototype
进行比较,它会返回 true
。
这是因为圆圈和矩形都是使用 Raphael.el中定义的元素原型制作的/ code>。然而,对于这些,拉斐尔让它变得简单:
someRectangle.type ==rect
someCircle.type ==circle
...等等。
Given a JavaScript object, how can I check if it is a Raphael object (not the paper, but a circle, path, etc.)?
Raphael.el
represents the generic element prototype; I think I want to test
x.__proto__ === Raphael.el
in a cross browser way, but I am not completely sure.
To elaborate a little and add some more relevant info (it took me a little while to figure out the accepted answer, and I'm clearly not alone looking at the other answers, also, the accepted answer only works for one type of Raphael object: it solves the original question, this is a more complete resource).
Detecting Raphael elements
Using x.constructor.prototype == Raphael.el
, you're taking x
, the variable that might be a Raphael element (circle, path etc - not a Raphael set
or paper
object) and comparing the prototype of the function that constructed it with the prototype for Raphael elements in Raphael itself (Raphael is a function object, el is a defined property of it).
This works, but it also won't find raphael objects based on different prototypes to Raphael.el, like sets and paper objects:
Detecting Raphael sets
If you wanted to test if something was a Raphael set, the set prototype is in Raphael.st
so you could test if a variable is a Raphael set using:
someSet.constructor.prototype == Raphael.st
Detecting Raphael paper objects
As for the equivalent for sniffing Raphael paper objects, since they are created using Raphael()
function, you can use:
paper.constructor.prototype == Raphael.prototype
The above three are basically the same as...
someSet.constructor.prototype == paper.circle().constructor.prototype
...or...
someSet.constructor.prototype == paper.set().constructor.prototype
...or...
someSet.constructor.prototype == Raphael().constructor.prototype
...but without actually running those functions, so avoiding wasted processing (and avoiding Raphael() complaining that it hasn't been passed an ID).
Detecting sub-types of object (e.g. rectangle, circle...)
None of the above works for subtypes of Raphael elements - e.g. if you compare a circle with R.rect().constructor.prototype
, it returns true
.
This is because both circles and rectangles are made using the element prototype defined in Raphael.el
. For these, however, Raphael makes it easy:
someRectangle.type == "rect"
someCircle.type == "circle"
...etc..
这篇关于如何检查是否有拉斐尔物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!