/********* module patten ***************/
var obj = obj || {};
obj.methods = (function () {
return {
fun1: function (str) {
alert("this is fun 1 " + str);
},
fun2: function (str) {
alert("this is fun 2 " + str);
}
}
})();
// call to module pattern
/*
obj.methods.fun1("called");
obj.methods.fun2("called");
*/
/******* revealing pattern ***********/
// high level of abstraction for a group of related functions
obj.methods1 = (function () {
var username = "empty";
function login(str) {
console.log("you are logegd in with user " + str);
username = str;
}
function logout(str) {
console.log("you are logged out");
username = null;
}
function getUser() {
console.log("the logged in user is " + username);
return username;
}
//expose function api to object
return {
login: login,
logout: logout,
getuser: getUser,
uname: username,
}
})();
//call to revealing pattern
/*
obj.methods1.login("don");
console.log(obj);
console.log(obj.methods1.getuser());
console.log(obj.methods1.uname);
*/
//*********** Import mixing model **********************************//
// value passed in IIFE
function EventService()
{
this.addEvent = function(evService){ console.log("You added an event :"+evService);}
}
var es = new EventService();
obj.methods2 = (function (eService) {
var username = "empty";
function login(str) {
console.log("you are logegd in with user " + str);
username = str;
eService.addEvent("fireEvent");
}
function logout(str) {
console.log("you are logged out");
username = null;
}
function getUser() {
console.log("the logged in user is " + username);
return username;
}
//expose function api to object
return {
login: login,
logout: logout,
getuser:...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.