开发者

How can I check if something is a Raphael object?

Given a JavaScript object, how can I check if it is a Raphael object (not the paper, but a circle, path, etc.)?

Raphael.el r开发者_运维百科epresents 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..


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

x.constructor.prototype == Raphael.el


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


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")
{
   ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜