Including Private Functions in Your Plugin - J Query

Problem
Your plugin code is growing and needs to be organized. How can you implement a private method that’s unavailable to code outside your plugin?

Solution

By utilizing the plugin design pattern started in Recipe 12.4, private functions may be defined normally within the anonymous function that we’ve wrapped our plugin in.
Because the function is enclosed in an anonymous function, outside code won’t see our private method. Code outside will only be able to see functions or methods that are attached to the jQuery object.

Discussion

Because we now have our plugin wrapped in an anonymous function, defining private functions within our plugin is as simple as adding a new function as you normally would.
Grouping and organizing your plugin with public and private methods offers many advantages to your users and to the plugin author. As your plugin matures and you receive feedback from the community, you can leverage the use of public and private methods to provide a consistent API between plugin versions. The consistency of your
API can be a major factor in your plugin’s success.
The ability to break code down into private and public messages also has significant advantages in code organization as your plugin grows. Well-organized code is easier to read, to maintain, and to test. Well-tested, clean code can lead to less error-prone code.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics