Skip to content

Instantly share code, notes, and snippets.

@micmath
micmath / 1.js
Created August 7, 2012 15:43
Chapter 1
function main() {
var count = 0;
}
console.log(count); // undefined
@micmath
micmath / beartrapJS.js
Created August 2, 2012 07:32
Examine the code snippet below, without evaluating it. What would you expect to be consoled?
MyApp = 42;
(function() {
if (typeof MyApp === 'undefined') {
var MyApp = { oops: true };
}
console.log(MyApp);
}());
@micmath
micmath / USE CASE
Created June 14, 2012 22:17
Case for separate module URLs:
Traffic analysis of your site shows that most visitors
require module A (big, almost never changes) and only
a few visitors require module B (small, changes often).
Keep those modules at separate URLs and everyone will
only download module A rarely. On subsequent pages/visits
that data will come straight from user-agent's cache.
Combine them into a single URL and everyone will have
to download modules A+B every single time module B
/*
* File: jquery.dataTables.js
* Version: 1.9.0.dev
* Description: Paginate, search and sort HTML tables
* Author: Allan Jardine (www.sprymedia.co.uk)
* Created: 28/3/2008
* Language: Javascript
* License: GPL v2 or BSD 3 point style
* Project: Mtaala
* Contact: [email protected]
/**
* Classic Atari PONG!
* @author Rob Taylor <[email protected]>
* @date 15/11/2011
*/
define([
'jquery-1'
], function ($) {
var Pong = function () {
function findMyOwnTag(mySrc) {
var scripts = document.getElementsByTagName('script');
for (var i = scripts.length-1; i >= 0; i--) {
if (scripts[i].src && scripts[i].src.indexOf(mySrc) > -1) {
return scripts[i]
}
}
}
myScriptNode = findMyOwnTag('bloader.js');
<script>
bloader = {
baseUrl: '/path/to/my/js',
pluginPath: 'for/some/reason/plugins/r/here',
paths: {
curl: 'curl/src/curl',
cssx: 'cssx/src/cssx',
my: '../../my-lib/'
},
apiName: 'someOtherName'
@micmath
micmath / gist:1354964
Created November 10, 2011 14:29
pkgjs

http://mojo.codehaus.org/js-import-plugin/

modules.json

{
    "jquery": {
        "src": "file://Users/michael/scripts/vendor/jquery/1.7.0/jquery.js"
    },
    "underscore": {

"src": "file://Users/michael/scripts/vendor/underscore/1.2.1/underscore.js",

@micmath
micmath / pattern1.js
Created October 31, 2011 23:28
Ways of documenting inner symbols inside anonymous functions that later get promoted to global
// Be explicit - provide names for everything.
(function(window) {
/**
* The ToolBox
*
* @class ToolBox
*/
var ToolBox = {};
/**
/**
* Attaches the script represented by the URL to the current
* environment. Right now only supports browser loading,
* but can be redefined in other environments to do the right thing.
* @param {String} url the url of the script to attach.
* @param {String} contextName the name of the context that wants the script.
* @param {moduleName} the name of the module that is associated with the script.
* @param {Function} [callback] optional callback, defaults to require.onScriptLoad
* @param {String} [type] optional type, defaults to text/javascript
*/