开发者

Why does Raphael use object[apply]?

开发者 https://www.devze.com 2023-01-29 19:31 出处:网络
I\'ve been looking through the Raphael s开发者_开发百科ource – source They appear to use object[apply](obj, args)

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()
0

精彩评论

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

关注公众号