"Modularity is the degree to which a system's components may be separated and recombined."
$(document).ready(function(){
$("ul.menu li span").click(function() {
$(this).parent().find("ul.level2").slideToggle('slow').show();
});
startAutoRefresh();
document.cookie = 'hasvisited=1';
$('.ad').each(function() {
OAS_AD($(this).attr('data-pos'));
});
$.getJSON('some.thing', function(res) {
$('.random-dom').text(res.numDays);
});
// [...]
});
Write programs that do one thing and do it well. Write programs to work together.
Write modules that do one thing and do it well. Write modules to work together.
// app-specific code...
if (parseInt(document.cookie.replace(/(?:(?:^|.*;\s*)numVisits\s*\=\s*([^;]*).*$)|^.*$/, "$1"), 10) == 0) {
// first visit
$('.annoying-we-have-app-dialog')
.removeClass('hidden')
.addClass('annoying-slide-in-effect');
document.cookie = 'numVisits=1; path=/; domain=.your-domain.com';
}
// More app-specific code
var Cookies = require('cookie-module')
, appPrompt = require('app-prompt');
var numVisits = Cookies.get('numVisits');
if (numVisits == 0) { // First visit
appPrompt.show();
}
Cookies.set('numVisits', ++numVisits, {
path: '/',
domain: '.your-domain.com'
});
var Cookie = {
set: function(name, value) {
document.cookie = name + '=' + escape(value);
},
get: function(name) {
var regex = new RegExp(
"(?:(?:^|.*;\\s*)" + name
+ "\\s*\\=\\s*([^;]*).*$)|^.*$"
);
return unescape(document.cookie.replace(regex, '$1'));
}
};
Cookie.set('foo', 'bar');
assert.equal(Cookie.get('foo'), 'bar');
A group with a goal of building up the JavaScript ecosystem for web servers, desktop, command line apps and the browser.- CommonJS.org
define(['cookies'], function(Cookies) {
var cookieName = 'numVisits';
return {
getNumberOfVisits: function() {
var visits = parseInt(Cookies.get(cookieName), 10);
return isNaN(visits) ? 0 : visits;
},
isFirstVisit: function() {
return this.getNumberOfVisits() === 0;
},
incrementVisitCount: function() {
var numVisits = this.getNumberOfVisits() + 1;
Cookies.set(cookieName, numVisits);
return numVisits;
}
};
});
var Cookies = require('cookies')
, cookieName = 'numVisits';
exports.getNumberOfVisits = function() {
var visits = parseInt(Cookies.get(cookieName), 10);
return isNaN(visits) ? 0 : visits;
};
exports.isFirstVisit = function() {
return this.getNumberOfVisits() === 0;
};
exports.incrementVisitCount = function() {
var numVisits = this.getNumberOfVisits() + 1;
Cookies.set(cookieName, numVisits);
return numVisits;
};
import { set as setCookie, get as getCookie } from 'cookies';
export var getNumberOfVisits = function() {
var visits = parseInt(getCookie(cookieName), 10);
return isNaN(visits) ? 0 : visits;
};
export var isFirstVisit = function() {
return this.getNumberOfVisits() === 0;
};
export var incrementVisitCount = function() {
var numVisits = this.getNumberOfVisits() + 1;
setCookie(cookieName, numVisits);
return numVisits;
};
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['cookies'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('cookies'));
} else {
root.VisitCounter = factory(root.Cookies);
}
}(this, function (Cookies) {
var cookieName = 'numVisits';
return {
getNumberOfVisits: function() { /* ... */ },
isFirstVisit: function() { /* ... */ },
incrementVisitCount: function() { /* ... */ }
};
}));