Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Last active December 16, 2015 17:19
Show Gist options
  • Save ryasmi/5469868 to your computer and use it in GitHub Desktop.
Save ryasmi/5469868 to your computer and use it in GitHub Desktop.
Things learned in JavaScript and they are good. Sourced mainly from Paul Irish (watched his video a while ago but I wanted to get it down in code).
// Asynchronous Recursion.
(function me() {
doStuff();
setTimeout(me, 100);
}());
// (1) Better than the alternative that never ends.
setInterval(function () { ... }, 100);
// Ajax example.
(function loopsiloopsiloo () {
doStuff;
$("#Update").load("awesomething.php", function () {
loopsiloopsiloo();
});
}());
// new Function.
var x = (new Function("return " + data))();
// (1) Better than using eval.
var x = eval("(" + data + ")");
// Immediately-Invoked Function Expression (IIFE).
(function (window, document, undefined) {
...
}(this, this.document));
// (1) When minified it becomes.
(function (A, B, C) {
...
}(this, this.document));
// (2) Ensures that undefined comparisons are correct. For example you might have...
undefined = true;
// (3) Benefits scope traversal because it only had to search the local scope for the variable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment