Skip to content

Commit f4aad2b

Browse files
arthurvragcolom
authored andcommitted
Plugin Creation: remove content regarding utility methods
Fixes jquerygh-396 Closes jquerygh-577
1 parent 36e1970 commit f4aad2b

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

page/plugins/basic-plugin-creation.md

+2-14
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,8 +44,6 @@ $.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

5349
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 `$`:
@@ -60,14 +56,6 @@ The `$` variable is very popular among JavaScript libraries, and if you're using
6056
return this;
6157
};
6258
63-
$.ltrim = function( str ) {
64-
return str.replace( /^\s+/, "" );
65-
};
66-
67-
$.rtrim = function( str ) {
68-
return str.replace( /\s+$/, "" );
69-
};
70-
7159
}( jQuery ));
7260
```
7361

0 commit comments

Comments
 (0)