Skip to content

Instantly share code, notes, and snippets.

@micmath
micmath / gist:1183504
Created August 31, 2011 13:09
Three proposals for a syntax to allow documenting symbols attached to JavaScript built-ins.
/**
Encrypt this string.
@function crypt
@member {@builtin String}.prototype
@see {@link {@builtin String}}
@param {string} salt
*/
String.prototype.crypt = function(salt) {
}
function ctm() { var u = 'undefined'; return ((typeof document.querySelector !== u) && (typeof localStorage !== u) && (typeof window.addEventListener !== u)) ? true : false;}
function ctm() {var u='undefined';return typeof document.querySelector!==u && typeof localStorage!==u && typeof window.addEventListener!==u ? true:false;}
function ctm(){return 'querySelector' in document && 'localStorage' in window && 'addEventListener' in window ? true:false}
function ctm(){try{return 'querySelector' in document && 'localStorage' in window && 'addEventListener' in window ? true:false;}catch(){return false}}
function ctm(){return 'querySelector' in document && 'localStorage' in window && 'addEventListener' in window}
@micmath
micmath / consumer.html
Created June 26, 2011 17:28
on domain a.example.com
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>post</title>
<script src="http://easyxdm.net/current/easyXDM.min.js"></script>
<script>
var socket = new easyXDM.Socket({
remote: "http://b.example.com/provider.html",
onReady: function(){
/**
* A module for retrieving information about Photos from Foursquare.
* @module "node-foursquare.Photos"
*/
/**
* Create a new Photos object.
* @constructor
* @param {Object} config A valid configuration.
/**
* Construct the Foursquare.Photos module.
* @param {Object} config A valid configuration.
* @module node-foursquare/Photos
*/
module.exports = function(config) {
var core = require("./core")(config),
logger = require('log4js')(config.log4js).getLogger("node-foursquare.Photos");
/**
@micmath
micmath / thisThat.js
Created May 23, 2011 13:28
What "this" references, inside inner functions.
var x = 1;
function Foo() {
this.x = 2;
function bar() {
console.log(this.x); //=> 1
}
bar();
var zop = function() {
@micmath
micmath / gist:976164
Created May 17, 2011 08:37
Trim leading whitespace from code examples
function escapeHtml(str) {
return str.toString().replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&(\s)/g, "&amp;$1");
}
function prepareCodeExample(str) {
str = str.toString();
//remove blank lines at the start and end of the example
var description = str.match(/^\s*.*\s*()/)
str = str.replace(/^(\s*[\r\n]+)+/, "").replace(/([\r\n]+\s+)+$/, "");
//read the space at the start for the 1st line of code
@micmath
micmath / gist:962372
Created May 9, 2011 11:27
Make the glow object accessible outside the onLoad callback
gloader.load(
["glow", "1"],
{
onLoad: function(glow) { // will run whenever the glow code arrives
// make this glow global
window.glow = glow;
}
}
);
Make two file requests for two modules
----
This is the current state of our RequireJS usage. The map is automatically
generated by the BBC Mapper application. This is flexible and it works, however
it requires two downloads from the server.
// map module ids to urls
require({
'swfobject-1': 'exact/path/1.2.3/swfobject.js',
@micmath
micmath / coolplug.js
Created March 11, 2011 11:32
Wrapping a plugin that modifies the jQuery object in a "sandbox" so that we can control exactly which jQuery the plugin modifies.
define('coolplug-1', function() {
// sandbox function
return function($) {
//---- usual plugin code starts here ----
$.fn.cool = function() { alert('cool!'); }
//---- usual plugin code ends here ------