Skip to content

Instantly share code, notes, and snippets.

View hshoff's full-sized avatar
📊

Harrison Shoff hshoff

📊
View GitHub Profile
// new crockford module1.js
(function(){
console.log('module1');
// return a function that logs it's
// arguments when invoked
return function(){
console.log(arguments);
};
}())
// new module1.js
(function() {
console.log('module1');
// return a function that logs it's
// arguments when invoked
return function(){
console.log(arguments);
};
})()
(function(){ return undefined; })()();
// => TypeError: undefined is not a function
// equivalent
(undefined)();
// crockford
(function(){ return undefined; }())();
// => TypeError: undefined is not a function
// problem
(function(){console.log('module1');})()(function(){console.log('module2');})();
// => module1
// => TypeError: undefined is not a function
// crockford problem
(function(){console.log('module1');}())(function(){console.log('module2');}());
// => module1
// => module2
// => TypeError: undefined is not a function
// module1.js
(function() {
console.log('module1');
})();
// module2.js
(function() {
console.log('module2');
})();
!function(){}(); // => true
~function(){}(); // => -1
+function(){}(); // => NaN
-function(){}(); // => NaN
// Crockford's preference - parens on the inside
(function() {
console.log('Welcome to the Internet. Please follow me.');
}());
(function() {
console.log('Welcome to the Internet. Please follow me.');
})();
@hshoff
hshoff / danger.js
Created October 17, 2012 20:13
Client Side Exception Logger
// Demo:
// -----------------------------------
// Copy & Paste into the console.
// Optional
// run test() => true
// run test(1) => Error
// newTest = wrap(test, {
// name: 'test',
// filename: '/js/danger.js'
// })
var sum = new Function('a', 'b', 'return a + b');