Skip to content

Commit 8b17b6b

Browse files
committed
Plugin Creation: remove content regarding utility methods
1 parent a22a743 commit 8b17b6b

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

page/plugins/basic-plugin-creation.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"attribution": [ "jQuery Fundamentals" ]
66
}</script>
77

8-
Sometimes you want to make a piece of functionality available throughout your code. For example, perhaps you want a single method you can call on a jQuery selection that performs a series of operations on the selection. Maybe you wrote a really useful utility function that you want to be able to move easily to other projects. In this case, you may want to write a plugin.
8+
Sometimes you want to make a piece of functionality available throughout your code. For example, perhaps you want a single method you can call on a jQuery selection that performs a series of operations on the selection. In this case, you may want to write a plugin.
99

10-
## How jQuery Works 101: jQuery Object Methods and Utility Methods
10+
## How jQuery Works 101: jQuery Object Methods
1111

1212
Before we write our own plugins, we must first understand a little about how jQuery works. Take a look at this code:
1313

@@ -17,8 +17,6 @@ $( "a" ).css( "color", "red" );
1717

1818
This is some pretty basic jQuery code, but do you know what's happening behind the scenes? Whenever you use the `$` function to select elements, it returns a jQuery object. This object contains all of the methods you've been using (`.css()`, `.click()`, etc.) and all of the elements that fit your selector. The jQuery object gets these methods from the `$.fn` object. This object contains all of the jQuery object methods, and if we want to write our own methods, it will need to contain those as well.
1919

20-
Additionally the jQuery utility method `$.trim()` is used above to remove any leading or trailing empty space characters from the user input. Utility methods are functions that reside directly in the `$` function itself. You may occasionally want to write a utility method plugin when your extension to the jQuery API does not have to do something to a set of DOM elements you've retrieved.
21-
2220
## Basic Plugin Authoring
2321

2422
Let's say we want to create a plugin that makes text within a set of retrieved elements green. All we have to do is add a function called `greenify` to `$.fn` and it will be available just like any other jQuery object method.
@@ -46,10 +44,10 @@ $.fn.greenify = function() {
4644
$( "a" ).greenify().addClass( "greenified" );
4745
```
4846

49-
Note that the notion of chaining is *not* applicable to jQuery utility methods like `$.trim()`.
50-
5147
## Protecting the $ Alias and Adding Scope
5248

49+
Note that the notion of chaining is *not* applicable to jQuery utility methods like `$.trim()`.
50+
5351
The `$` variable is very popular among JavaScript libraries, and if you're using another library with jQuery, you will have to make jQuery not use the `$` with `jQuery.noConflict()`. However, this will break our plugin since it is written with the assumption that `$` is an alias to the `jQuery` function. To work well with other plugins, _and_ still use the jQuery `$` alias, we need to put all of our code inside of an [Immediately Invoked Function Expression](http://stage.learn.jquery.com/javascript-101/functions/#immediately-invoked-function-expression-iife), and then pass the function `jQuery`, and name the parameter `$`:
5452

5553
```

0 commit comments

Comments
 (0)