MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/1v46kg/how_to_create_singleton_objects_in_javascript/ceoqkc5/?context=3
r/javascript • u/viralpatelnet • Jan 13 '14
6 comments sorted by
View all comments
9
Ahem...
var singleton = (function() { var privateData = "foo"; var privilegedMethod = function() { return privateData; }; var why = { not: "just", create: "a", literal: "object", with: privilegedMethod }; return why; }()); console.log(singleton.privateData); // => undefined console.log(singleton.with()); // => "foo"
1 u/cwmma Jan 13 '14 If you did Object.create instead of object literal notation it would have a prototype.If we're going all out for over engineering an iife
1
If you did Object.create instead of object literal notation it would have a prototype.
If we're going all out for over engineering an iife
9
u/[deleted] Jan 13 '14 edited Jan 13 '14
Ahem...