Skip to content

Instantly share code, notes, and snippets.

@gufranco-zz
Last active March 11, 2024 13:50
Show Gist options
  • Save gufranco-zz/5876078 to your computer and use it in GitHub Desktop.
Save gufranco-zz/5876078 to your computer and use it in GitHub Desktop.
JavaScript object prototype
;$(function() {
var fooBar = new FooBar(); // constructor
fooBar.foo(); // foo
fooBar.bar(); // bar
});
/**
* FooBar object
*
* @author Gustavo Franco
* @since 2013-06-27
*/
;var FooBar = (function($, window, document, undefined) {
'use strict';
/**
* Constructor method
*
* @author Gustavo Franco
* @since 2013-06-27
*/
function FooBar() {
try {
console.log('constructor');
} catch (exception) {
console.error(exception.message);
}
}
/**
* Foo method
*
* @author Gustavo Franco
* @since 2013-06-27
*/
FooBar.prototype.foo = function() {
try {
console.log('foo');
} catch (exception) {
console.error(exception.message);
}
};
/**
* Bar method
*
* @author Gustavo Franco
* @since 2013-06-27
*/
FooBar.prototype.bar = function() {
try {
console.log('bar');
} catch (exception) {
console.error(exception.message);
}
};
return FooBar;
})(jQuery, window, document);
<!DOCTYPE html>
<html>
<head>
<title>Object prototype example</title>
</head>
<body>
<p>Open the console ;)</p>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="FooBar.js"></script>
<script src="bootstrap.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment