Skip to content

Instantly share code, notes, and snippets.

@dsimard
dsimard / singleton.js
Created August 22, 2011 23:53
Javascript singleton
function Cats() {
var names = [];
// Get the instance of the Cats class
// If there's none, instanciate one
var getInstance = function() {
if (!Cats.singletonInstance) {
Cats.singletonInstance = createInstance();
}
@dsimard
dsimard / is a hash.js
Created January 6, 2011 19:39
How to know if an object is a hash
function isHash(obj) {
return typeof(obj) == "object" && Object.prototype.toString.call(obj) != "[object Array]";
}
@dsimard
dsimard / add a submodule on github.sh
Created October 22, 2010 19:09
Add a submodule on github
git submodule add git://github.com/[user]/[project].git [submodule path]
@dsimard
dsimard / 2 levels deep.js
Created October 19, 2010 21:12
2 levels deep
var myApplication = {};
myApplication.instance = {
msg : "Hello world!",
hello : function() {
alert(myApplication.instance.msg);
}
}
@dsimard
dsimard / Simple singleton.js
Created October 19, 2010 20:59
Simple singleton
var instance = {
msg : "Hello world!",
hello : function() {
alert(instance.msg);
}
}
instance.hello();
@dsimard
dsimard / singletons in javascript.js
Created October 19, 2010 15:50
Real javascript singleton
var myApplication = {};
myApplication.instance = (function() {
var i = {
msg : "Hello world!",
hello : function() {
alert(i.msg);
}
};
@dsimard
dsimard / CountTo10closure.js
Created May 19, 2010 14:50
Count to 10 recursively with closures
var count = function countToTen(i) {
if (i <= 10) {
console.log(i);
count(i+1); // Call count with the power of closures
}
}
count(1);
@dsimard
dsimard / CountTo10anonymous.js
Created May 19, 2010 14:38
recursive count to 10 with named functions
var count = function countToTen(i) {
if (i <= 10) {
console.log(i);
countToTen(i+1); // You can cleanly call the named function
}
}
count(1);
@dsimard
dsimard / countTo10anon.js
Created May 19, 2010 14:36
recursive count to 10 anonymous
var count = function(i) {
if (i <= 10) {
console.log(i);
arguments.callee(i+1); // this will throw an error on ECMAScript 5 strict
}
}
count(1);
@dsimard
dsimard / named.js
Created May 19, 2010 14:28
named functions
var func = function f1() {
var subfunc = function f2() {
(function f3() {
console.trace();
})()
}
subfunc();
}
func();