Skip to content

Instantly share code, notes, and snippets.

@rishabhgupta
Last active May 8, 2018 22:56
Show Gist options
  • Save rishabhgupta/ace5f62c19f4f62bb23dacdc196889ec to your computer and use it in GitHub Desktop.
Save rishabhgupta/ace5f62c19f4f62bb23dacdc196889ec to your computer and use it in GitHub Desktop.
Javascript Design Patterns
var prefixes = ['hello', 'hi', 'ola'],
quotes = [
"Set a goal that makes you want to jump out of bed in the morning",
"When you start each day with a grateful heart…",
"I opened two gifts this morning. They were my eyes.",
"Some days you just have to create your own sunshine"
],
said = [],
print = function(msg){
said.push('<div>' + msg + '</div>');
var output = '';
for (var iter in said){
output += said[iter];
}
$('.target').html(output);
},
generateGreeting = function () {
var dateTime = new Date(),
hours = dateTime.getHours();
if (hours < 12){
return 'Good Morning.';
} else if (hours >= 12 && hours <= 6){
return 'Good Afternoon.';
} else {
return 'Good Night.';
}
}
,
greet = function (name) {
var pre = prefixes[ Math.floor(Math.random() * prefixes.length) ],
qoute = quotes[ Math.floor(Math.random() * quotes.length) ],
output = '';
output += pre + '! ' + name + ' ' + generateGreeting() + ' ' + qoute;
print(output);
};
$(document).ready(function () {
greet('Cipherhack');
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Design Patters</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div class="article">
<h1>Design Patterns</h1>
<div class="target"></div>
</div>
</body>
</html>
var cipherhackModule = (function () {
/*Private scope, anything that inside here lives within the function
* Only things within the function will have access including returx
*/
var prefixes = ['hello', 'hi', 'ola'],
quotes = [
"Set a goal that makes you want to jump out of bed in the morning",
"When you start each day with a grateful heart…",
"I opened two gifts this morning. They were my eyes.",
"Some days you just have to create your own sunshine"
],
said = [];
function print(msg){
said.push('<div>' + msg + '</div>');
var output = '';
for (var iter in said){
output += said[iter];
}
$('.target').html(output);
}
function generateGreeting() {
var dateTime = new Date(),
hours = dateTime.getHours();
if (hours < 12){
return 'Good Morning.';
} else if (hours >= 12 && hours <= 6){
return 'Good Afternoon.';
} else {
return 'Good Night.';
}
}
return {
// Public Methodss
greet: function (name) {
var pre = prefixes[ Math.floor(Math.random() * prefixes.length) ],
qoute = quotes[ Math.floor(Math.random() * quotes.length) ],
output = '';
output += pre + '! ' + name + ' ' + generateGreeting() + ' ' + qoute;
print(output);
}
}
})();
$(document).ready(function () {
cipherhackModule.greet('Cipherhack');
});
var cipherhackModule = (function () {
var _prefixes = ['hello', 'hi', 'ola'],
_quotes = [
"Set a goal that makes you want to jump out of bed in the morning",
"When you start each day with a grateful heart…",
"I opened two gifts this morning. They were my eyes.",
"Some days you just have to create your own sunshine"
],
_said = [];
function _print(msg){
_said.push('<div>' + msg + '</div>');
var output = '';
for (var iter in _said){
output += _said[iter];
}
$('.target').html(output);
}
function _generateGreeting() {
var dateTime = new Date(),
hours = dateTime.getHours();
if (hours < 12){
return 'Good Morning.';
} else if (hours >= 12 && hours <= 6){
return 'Good Afternoon.';
} else {
return 'Good Night.';
}
}
function greet(name) {
var pre = _prefixes[ Math.floor(Math.random() * _prefixes.length) ],
qoute = _quotes[ Math.floor(Math.random() * _quotes.length) ],
output = '';
output += pre + '! ' + name + ' ' + _generateGreeting() + ' ' + qoute;
_print(output);
}
return {
// Public Methodss
greet:greet
}
})();
$(document).ready(function () {
cipherhackModule.greet('Cipherhack');
});
var com = com || {};
com.cipherhack = com.cipherhack || {};
com.cipherhack.designPattern = com.cipherhack.designPattern || {};
com.cipherhack.designPattern.namespace = com.cipherhack.designPattern.namespace || {};
com.cipherhack.designPattern.namespace = {
prefixes: ['hello', 'hi', 'ola'],
...
...
}
$(document).ready(function () {
com.cipherhack.designPattern.namespace.greet('Cipherhack');
});
var cipherhackApp = {
prefixes: ['hello', 'hi', 'ola'],
quotes: [
"Set a goal that makes you want to jump out of bed in the morning",
"When you start each day with a grateful heart…",
"I opened two gifts this morning. They were my eyes.",
"Some days you just have to create your own sunshine"
],
said: [],
print: function(msg){
this.said.push('<div>' + msg + '</div>');
var output = '';
for (var iter in this.said){
output += this.said[iter];
}
$('.target').html(output);
},
generateGreeting: function () {
var dateTime = new Date(),
hours = dateTime.getHours();
if (hours < 12) {
return 'Good Morning.';
} else if (hours >= 12 && hours <= 6) {
return 'Good Afternoon.';
} else {
return 'Good Night.';
}
},
greet: function (name) {
var pre = this.prefixes[ Math.floor(Math.random() * this.prefixes.length) ],
qoute = this.quotes[ Math.floor(Math.random() * this.quotes.length) ],
output = '';
output += pre + '! ' + name + ' ' + this.generateGreeting() + ' ' + qoute;
this.print(output);
}
}
$(document).ready(function () {
cipherhackApp.greet('Cipherhack');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment