How can I check if something is a Raphael object?

时间秒杀一切 提交于 2019-12-04 03:20:53

问题


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.


回答1:


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..




回答2:


Pablo posted an answer that was not quite correct but gave me inspiration towards finding a correct solution:

x.constructor.prototype == Raphael.el



回答3:


Can't you use the constructor property and check against the function that created the object (I'm assuming it's called Raphael but I haven't used the lib).

Edit

Checked the lib site, you actually do it that way:

obj.constructor === Raphael //true




回答4:


I wasnt able to use the mentioned answer. But what worked for me was to compare explicitly with string "Raphaël’s object".

Eg:

var textName = paper.getElementByPoint(e.pageX, e.pageY);
if (textName== "Raphaël’s object")
{
   ...
}


来源:https://stackoverflow.com/questions/6475689/how-can-i-check-if-something-is-a-raphael-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!