Skip to content

Commit fb999ca

Browse files
committed
Added section on not cluttering the $.fn namespace
1 parent 1ed57d0 commit fb999ca

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

content/plugins/basic-plugin-creation.md

+32
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@ In addition, the primary purpose of an Immediately Invoked Function is to allow
7070
}
7171
}(jQuery));
7272
</javascript>
73+
74+
##Minimizing Plugin Footprint
75+
76+
It's good practice when writing plugins to only take up one slot within `$.fn`. This reduces both the chance that your plugin will be overriden, and the chance that your plugin will override other plugins. In other words, this is bad:
77+
78+
<javascript>
79+
(function ($) {
80+
$.fn.openPopup = function () {
81+
// Open popup code
82+
};
83+
84+
$.fn.closePopup = function () {
85+
// Close popup code
86+
};
87+
88+
}(jQuery));
89+
</javascript>
90+
91+
It would be much better to have one slot, and use parameters to control what action that one slot performs.
92+
93+
<javascript>
94+
(function ($) {
95+
$.fn.popup = function (action) {
96+
if( action === 'open') {
97+
// Open popup code
98+
} if( action === 'close' ) {
99+
// Close popup code
100+
}
101+
102+
};
103+
}(jQuery));
104+
</javascript>
73105

74106
##Using the each() method
75107

0 commit comments

Comments
 (0)