Why does Raphael use object[apply]?
I've been looking through the Raphael s开发者_开发百科ource – source
They appear to use
object[apply](obj, args)
which I am assuming is equivalent to
object.apply(obj, args)
Am I assuming correctly? And if so why do they do this?
Thanks
Edit
As @deceze says apply is a variable and therefore it is not equivalent. However, on line 38 they assign the string "apply"
to the variable apply
so this makes it equivalent. Why would you do this?
Using the dot notation is really just syntactic sugar for for object[propertyname]
, but it has one disadvantage, the property name cannot be minified.
By doing
var apply = "apply";
foobar[apply](.....
foobar[apply](.....
foobar[apply](.....
foobar[apply](.....
this can actually be minified to
var a = "apply";
b[a](.....
b[a](.....
b[a](.....
b[a](.....
And there's your reason, Raphael uses the [] notation in order to provide better minification.
In that case apply
should be a variable which holds the name of a method, so this is a way of calling object methods with variable names. As such it is not equivalent to object.apply()
, since this always calls the apply()
method.
var apply = 'foo';
object[apply](); // calls object.foo()
精彩评论