Skip to content

Instantly share code, notes, and snippets.

@oiler
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oiler/92e7cabb63c768c2720a to your computer and use it in GitHub Desktop.
Save oiler/92e7cabb63c768c2720a to your computer and use it in GitHub Desktop.
jQuery plugin authoring
<!--
http://blog.teamtreehouse.com/writing-your-own-jquery-plugins
http://programmers.stackexchange.com/questions/160732/function-declaration-as-var-instead-of-function
-->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport", content="width=device-width">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<h1>Hello, World</h1>
<h2>Hello, World</h2>
<h3>Hello, World</h3>
<script>
(function($) {
// jquery object dot notation
$.fn.pizza = function( customText ) {
$(this).text( customText );
}
// statement, is hoisted
function slice( customText ) {
$('h2').text( customText );
}
// expression, you define when and what it uses
var pie = function( customText ) {
$('h3').text( customText );
}
$(document).ready(function(){
$('h1').pizza('Hello, Pizza');
slice('Hello, Slice');
pie('Hello, Pie');
});
}(jQuery));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment