Skip to content

Instantly share code, notes, and snippets.

View nzakas's full-sized avatar
💭
GitHub time is limited currently. Please be patient.

Nicholas C. Zakas nzakas

💭
GitHub time is limited currently. Please be patient.
View GitHub Profile
@nzakas
nzakas / gist:1119343
Created August 2, 2011 00:38
Autofocus shim
<!-- autofocus shim for older browsers that doesn't impact newer ones -->
<input type="text" name="q" id="q" autofocus>
<script>
(function(textbox){
if (textbox.autofocus !== true){
textbox.focus();
}
})(document.getElementById("q"));
</script>
@nzakas
nzakas / stack.js
Created September 12, 2011 19:24
Stack implementation using ES6 proxies
/*
* Another ES6 Proxy experiment. This one creates a stack whose underlying
* implementation is an array. The proxy is used to filter out everything
* but "push", "pop", and "length" from the interface, making it a pure
* stack where you can't manipulate the contents.
*/
var Stack = (function(){
var stack = [],
@nzakas
nzakas / gist:1367412
Created November 15, 2011 15:54
Npm debug Log
info it worked if it ends with ok
verbose cli [ 'node', '/usr/local/bin/npm', 'update', 'npm', '-g' ]
info using npm@1.0.10
info using node@v0.4.10
verbose config file /home/nicholas/.npmrc
verbose config file /usr/local/etc/npmrc
silly exec /usr/local/bin/node "/usr/local/lib/node_modules/npm/bin/npm-get-uid-gid.js" "nobody" 1000
silly output from getuid/gid {"uid":65534,"gid":1000}
silly output from getuid/gid
silly testEngine { name: 'csslint',
@nzakas
nzakas / namespace.js
Created December 6, 2011 19:14
A single global with a namespace method
//BSD Licensed
var YourGlobal = {
namespace: function(ns){
var parts = ns.split("."),
object = this,
i, len;
for (i=0, len=parts.length; i < len; i++) {
if (!object[parts[i]]) {
@nzakas
nzakas / gzip.xml
Created December 23, 2011 15:51
Ant target for gzipping multiple files
<target name="compress">
<!-- store filenames in a property delimited by ; -->
<pathconvert pathsep=";" property="compress.jsfiles">
<fileset dir="${build.dir}" includes="*.js"/>
</pathconvert>
<script language="javascript"><![CDATA[
importPackage(java.io);
@nzakas
nzakas / button-issue.htm
Created February 7, 2012 23:47
Firefox buttons too large
<!DOCTYPE html>
<html>
<head>
<title>Button Issue</title>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?3.4.1/build/cssfonts/cssfonts-min.css&3.4.1/build/cssreset/cssreset-min.css">
<style>
p {
margin: 10px;
@nzakas
nzakas / mjstoc.md
Created April 17, 2012 14:50
Maintainable JavaScript Table of Contents
  • Part 1 - Style Guide
    • Chapter 1 - Basic Formatting
    • Chapter 2 - Comments
    • Chapter 3 - Statements
    • Chapter 4 - Functions and Expressions
  • Part 2 - Programming Practices
    • Chapter 5 - Loose Coupling
    • Chapter 6 - Avoid Globals
    • Chapter 7 - Event Handling
  • Chapter 8 - Avoid Null Comparisons
@nzakas
nzakas / noanon.md
Created June 8, 2012 21:19
Avoiding "new anonfn"

JSLint doesn't like using new function. This makes sense, because you're just creating a singleton, and so there's no point in using a constructor for that. If you have something like this:

var NowWithClosures = Backbone.Model.extend(new function(){
    var x = 1;
    this.initialize = function(){
        console.log('init');
    };
    this.AddOneToX = function(){
 x++;
@nzakas
nzakas / spawn.js
Created August 24, 2012 23:56
Devil spawn
#!/usr/bin/env node
var child = require('child_process');
var cp = child.spawn("dir", [""]);
cp.stdout.on("data", function(data) {
console.log(data.toString());
});
cp.stderr.on("data", function(data) {
console.error(data.toString());
@nzakas
nzakas / contest.js
Created November 23, 2012 22:21
Contest
/*
* Contest to win one of my books. If you're in the United States, you can choose either
* Professional JavaScript for Web Developers or Maintainable JavaScript and I will send
* you a signed copy. If you're outside the United States, I will send you the PDF version
* of either book. Good luck!
*
* Rules
* 1. Assume this is the only code! Don't worry about real world situations.
* 2. You can use anything defined in ECMAScript 5 to solve the problem (no DOM, BOM, etc.)
* 3. There is more than one correct answer but only the first correct answer wins.