Another option is to use a Module pattern , which has the advantages of keeping the breading entities truly private. Not everyone is a fan of the module template, though, since it prevents the monkey patch. This is especially problematic when you use the library, and you need to change the behavior, but you do not want to edit the source files to minimize upgrade problems. http://snook.ca/archives/javascript/no-love-for-module-pattern
var IOBreadcrumb = (function() { var breadcrumbs = []; return { add: function(title, url) { breadcrumbs.push({ title: title, url: url }); }, each: function (callback) { for (var i=0; i < breadcrumbs.length; i++) { callback(breadcrumbs[i].title, breadcrumbs[i].url); } } } })(); IOBreadcrumb.add('title A', 'http://url.com/A'); IOBreadcrumb.add('title B', 'http://url.com/B'); IOBreadcrumb.add('title C', 'http://url.com/C'); IOBreadcrumb.each(function(title, url){ console.log('Title', title, 'Url', url); });
source share