Skip to content

Instantly share code, notes, and snippets.

@nielk
Last active August 29, 2015 14:01
Show Gist options
  • Save nielk/6f92ee7e5ff081bfcea0 to your computer and use it in GitHub Desktop.
Save nielk/6f92ee7e5ff081bfcea0 to your computer and use it in GitHub Desktop.
javascript pattern with jquery
var myModule = (function($, window) {
// private property
var myProp = "hello";
// private methods
function myPrivateMethod() {
alert($('*').length);
};
// public methods
return {
init: function() {
alert(myProp);
myProp = "Goodbye";
},
myPublicMethod: function() {
alert('public method');
},
myPublicMethod2: function() {
myPrivateMethod();
alert(window.Object);
alert(myProp);
}
};
})($, window);
myModule.init();
myModule.myPublicMethod();
myModule.myPublicMethod2();
;(function(window) {
var Book = function( id, title, author, genre, pageCount,publisherID, ISBN, checkoutDate, checkoutMember, dueReturnDate,availability ){
this.id = id;
this.title = title;
this.author = author;
this.genre = genre;
this.pageCount = pageCount;
this.publisherID = publisherID;
this.ISBN = ISBN;
this.checkoutDate = checkoutDate;
this.checkoutMember = checkoutMember;
this.dueReturnDate = dueReturnDate;
this.availability = availability;
};
Book.prototype = {
getTitle: function () {
return this.title;
},
getAuthor: function () {
return this.author;
},
getISBN: function (){
return this.ISBN;
},
// For brevity, other getters are not shown
updateCheckoutStatus: function( bookID, newStatus, checkoutDate , checkoutMember, newReturnDate ){
this.id = bookID;
this.availability = newStatus;
this.checkoutDate = checkoutDate;
this.checkoutMember = checkoutMember;
this.dueReturnDate = newReturnDate;
},
extendCheckoutPeriod: function( bookID, newReturnDate ){
this.id = bookID;
this.dueReturnDate = newReturnDate;
},
isPastDue: function(bookID){
var currentDate = new Date();
return currentDate.getTime() > Date.parse( this.dueReturnDate );
}
};
window.Book = Book;
})(window);
var book = new Book(21, "a book title");
alert(book.getTitle());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment