开发者

Making a method public in JavaScript. Why this syntax?

I was studying TinyMCE code and stumbled upon this way of exposing public methods:

tinymce.extend(this, {
    execCommand : execCommand,
    queryCommandState : queryCommandState,
    queryCommandValue : queryCommandValue,
    addCommands : addCommands
});

What is the benefit of writing the above if the belo开发者_JAVA百科w code can be used instead (with fewer lines of code and less execution time required for the same task!)

this.execCommand = execCommand;
this.queryCommandState = queryCommandState;
this.queryCommandValue = queryCommandValue;
this.addCommands = addCommands;

Or even shorter, somewhere in the declaration of an object:

execCommand: execCommand,
queryCommandState: queryCommandState,
queryCommandValue: queryCommandValue,
addCommands: addCommands

Where's the catch?


Well, one thing that jumps out at me is the first sample that you have there is the method in which the TinyMCE expects its arguments for its extend function.

Glancing at the source of extend, it checks each key value pair for undefined, only adding them to the object if they're defined. So, there's a little bit of added functionality that can be useful when extending a class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜