3

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 below 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?

Andy
  • 2,670
  • 3
  • 30
  • 49

1 Answers1

2

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.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
  • correct, it expects a list of objects the given object is to be extended with. This is the way they publish their private methods, but my question is why publish that way, and not a simpler one? – Andy May 25 '11 at 05:40
  • Thanks Demian, to me this just means that in the case of a typo in the name of the method it will take more time to identify an error. But I guess it's hard to argue with tinymce creators unless one has done a job of a similar complexity. – Andy May 25 '11 at 05:48