Skip to content

Instantly share code, notes, and snippets.

@mannuelf
Forked from anonymous/index.html
Created April 21, 2016 06:54
Show Gist options
  • Save mannuelf/0aeb9cb7d00a34321d3b77911bda834a to your computer and use it in GitHub Desktop.
Save mannuelf/0aeb9cb7d00a34321d3b77911bda834a to your computer and use it in GitHub Desktop.
Basic Plugin Pattern Return Value return this; return this.each(); return.this.pushStack(); to maintain chainability // source http://jsbin.com/sacoba
<!DOCTYPE html>
<html>
<head>
<title>Basic Plugin Pattern Return Value</title>
<meta name="description" content="return this; return this.each(); return.this.pushStack(); to maintain chainability">
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="div1">A Div</div>
<div id="div2">Another Div</div>
<script id="jsbin-javascript">
(function($){
$.fn.myPlugin = function() {
var newArray = [this[1]];
console.log('First', newArray);
return this.pushStack(newArray);
};
})(jQuery);
$(function() {
console.log('Second', $('div').myPlugin());
console.log('Third', $('div').myPlugin().end().end());
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">(function($){
$.fn.myPlugin = function() {
var newArray = [this[1]];
console.log('First', newArray);
return this.pushStack(newArray);
};
})(jQuery);
$(function() {
console.log('Second', $('div').myPlugin());
console.log('Third', $('div').myPlugin().end().end());
});
</script></body>
</html>
(function($){
$.fn.myPlugin = function() {
var newArray = [this[1]];
console.log('First', newArray);
return this.pushStack(newArray);
};
})(jQuery);
$(function() {
console.log('Second', $('div').myPlugin());
console.log('Third', $('div').myPlugin().end().end());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment