Created
December 23, 2010 06:51
-
-
Save creationix/752667 to your computer and use it in GitHub Desktop.
a custom object inspector to help me understand JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function escapeString(string) { | |
string = string.replace(/\\/g, "\\\\"). | |
replace(/\n/g, "\\n"). | |
replace(/\r/g, "\\r"). | |
replace(/\t/g, "\\t"); | |
if (string.indexOf("'") < 0) { | |
return "'" + string + "'"; | |
} | |
string = string.replace(/"/g, "\\\""); | |
return '"' + string + '"'; | |
} | |
function escapeKey(string, enumerable) { | |
if (!enumerable) { return "[" + string + "]"; } | |
if ((/[a-z_$][a-z0-9_$]*/i).test(string)) { | |
return string; | |
} | |
return escapeString(string); | |
} | |
function getType(object) { | |
var proto = object.__proto__; | |
if (!(proto && proto.constructor)) { return ""; } | |
return proto.constructor.name; | |
} | |
function inspect(value, keyer, short) { | |
if (value === undefined) { return "undefined"; } | |
if (value === null) { return "null"; } | |
if (value === true) { return "true"; } | |
if (value === false) { return "false"; } | |
switch (typeof value) { | |
case 'object': | |
case 'function': | |
if (short) { | |
return "(" + getType(value) + (Object.keys(value).length ? "..." : "") + ")"; | |
} | |
var type = getType(value); | |
var parts = keyer(value).map(function (key, i) { | |
var prop = Object.getOwnPropertyDescriptor(value, key); | |
if (prop.hasOwnProperty("value")) { | |
return (i.toString() !== key ? escapeKey(key, prop.enumerable) + ": " : "") + | |
inspect(prop.value, keyer, true); | |
} | |
if (prop.get) { | |
if (prop.set) { | |
return escapeKey(key, prop.enumerable) + ": [Getter/Setter]"; | |
} | |
return escapeKey(key, prop.enumerable) + ": [Getter]"; | |
} else if (prop.set) { | |
return escapeKey(key, prop.enumerable) + ": [Setter]"; | |
} | |
}); | |
var body; | |
if (parts.length) { | |
body = " " + parts.join(", ") + " "; | |
if (body.length > 77 - type.length) { | |
body = "\n " + parts.join(",\n ") + "\n"; | |
} | |
} else { | |
body = ""; | |
} | |
return "(" + type + body + ")"; | |
case 'number': return value.toString(); | |
case 'string': return escapeString(value); | |
} | |
throw new Error("Not Implemented"); | |
} | |
var types = { | |
Number: [Number, 42], | |
String: [String, "Hello"], | |
Date: [Date, new Date(Date.now())], | |
RegExp: [RegExp, new RegExp("Find Me", "gi")], | |
Object: [Object, {name: "Tim", age: 28}], | |
Array: [Array, [1,2,3,4]], | |
Buffer: [Buffer, new Buffer(4)], | |
EventEmitter: [process.EventEmitter, process] | |
} | |
Object.keys(types).map(function (name) { | |
var type = types[name][0], | |
prototype = type.prototype, | |
example = types[name][1]; | |
console.log("%s -> %s", name, inspect(type, Object.getOwnPropertyNames)); | |
console.log("%s.prototype -> %s", name, inspect(prototype, Object.getOwnPropertyNames)); | |
console.log("example %s -> %s\n", name, inspect(example, Object.getOwnPropertyNames)); | |
}); | |
var modules = ["http", "net", "url", "path", "querystring", "child_process", "step", "connect", "haml", "nstore"]; | |
modules.map(function (name) { | |
console.log("%s -> %s", name, inspect(require(name), Object.getOwnPropertyNames)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Number -> (Function | |
[NaN]: NaN, | |
[arguments]: null, | |
[NEGATIVE_INFINITY]: -Infinity, | |
[POSITIVE_INFINITY]: Infinity, | |
[length]: 1, | |
[name]: 'Number', | |
[MAX_VALUE]: 1.7976931348623157e+308, | |
[prototype]: (Object), | |
[caller]: null, | |
[MIN_VALUE]: 5e-324 | |
) | |
Number.prototype -> (Object | |
[toExponential]: (Function), | |
[toString]: (Function), | |
[toLocaleString]: (Function), | |
[toPrecision]: (Function), | |
[valueOf]: (Function), | |
[constructor]: (Function), | |
[toFixed]: (Function) | |
) | |
example Number -> 42 | |
String -> (Function | |
[arguments]: null, | |
[length]: 1, | |
[name]: 'String', | |
[prototype]: (Object), | |
[fromCharCode]: (Function), | |
[caller]: null | |
) | |
String.prototype -> (Object | |
[length]: 0, | |
[constructor]: (Function), | |
[concat]: (Function), | |
[localeCompare]: (Function), | |
[substring]: (Function), | |
[italics]: (Function), | |
[charCodeAt]: (Function), | |
[strike]: (Function), | |
[indexOf]: (Function), | |
[toLowerCase]: (Function), | |
[trimRight]: (Function), | |
[toString]: (Function), | |
[toLocaleLowerCase]: (Function), | |
[replace]: (Function), | |
[toUpperCase]: (Function), | |
[fontsize]: (Function), | |
[trim]: (Function), | |
[split]: (Function), | |
[substr]: (Function), | |
[sub]: (Function), | |
[charAt]: (Function), | |
[blink]: (Function), | |
[lastIndexOf]: (Function), | |
[sup]: (Function), | |
[fontcolor]: (Function), | |
[valueOf]: (Function), | |
[link]: (Function), | |
[bold]: (Function), | |
[anchor]: (Function), | |
[trimLeft]: (Function), | |
[small]: (Function), | |
[search]: (Function), | |
[fixed]: (Function), | |
[big]: (Function), | |
[match]: (Function), | |
[toLocaleUpperCase]: (Function), | |
[slice]: (Function) | |
) | |
example String -> 'Hello' | |
Date -> (Function | |
[now]: (Function), | |
[arguments]: null, | |
[UTC]: (Function), | |
[length]: 7, | |
[name]: 'Date', | |
[prototype]: (Object), | |
[caller]: null, | |
[parse]: (Function) | |
) | |
Date.prototype -> (Object | |
[constructor]: (Function), | |
[toUTCString]: (Function), | |
[setMinutes]: (Function), | |
[setUTCMonth]: (Function), | |
[getMilliseconds]: (Function), | |
[getTime]: (Function), | |
[getMinutes]: (Function), | |
[getUTCHours]: (Function), | |
[toString]: (Function), | |
[setUTCFullYear]: (Function), | |
[setMonth]: (Function), | |
[getUTCMinutes]: (Function), | |
[getUTCDate]: (Function), | |
[setSeconds]: (Function), | |
[toLocaleDateString]: (Function), | |
[getMonth]: (Function), | |
[toTimeString]: (Function), | |
[toLocaleTimeString]: (Function), | |
[setUTCMilliseconds]: (Function), | |
[setYear]: (Function), | |
[getUTCFullYear]: (Function), | |
[getFullYear]: (Function), | |
[getTimezoneOffset]: (Function), | |
[setDate]: (Function), | |
[getUTCMonth]: (Function), | |
[getHours]: (Function), | |
[toLocaleString]: (Function), | |
[toISOString]: (Function), | |
[toDateString]: (Function), | |
[getUTCSeconds]: (Function), | |
[valueOf]: (Function), | |
[setUTCMinutes]: (Function), | |
[getUTCDay]: (Function), | |
[toJSON]: (Function), | |
[setUTCDate]: (Function), | |
[setUTCSeconds]: (Function), | |
[getYear]: (Function), | |
[getUTCMilliseconds]: (Function), | |
[getDay]: (Function), | |
[setFullYear]: (Function), | |
[setMilliseconds]: (Function), | |
[setTime]: (Function), | |
[setHours]: (Function), | |
[getSeconds]: (Function), | |
[toGMTString]: (Function), | |
[getDate]: (Function), | |
[setUTCHours]: (Function) | |
) | |
example Date -> (Date) | |
RegExp -> (Function | |
[$*]: [Getter/Setter], | |
$3: [Getter/Setter], | |
[$`]: [Getter/Setter], | |
[arguments]: null, | |
$9: [Getter/Setter], | |
rightContext: [Getter/Setter], | |
multiline: [Getter/Setter], | |
[length]: 2, | |
$7: [Getter/Setter], | |
lastParen: [Getter/Setter], | |
[$input]: [Getter/Setter], | |
[$+]: [Getter/Setter], | |
[$&]: [Getter/Setter], | |
[name]: 'RegExp', | |
leftContext: [Getter/Setter], | |
$8: [Getter/Setter], | |
$4: [Getter/Setter], | |
[prototype]: (Object), | |
$1: [Getter/Setter], | |
[$']: [Getter/Setter], | |
[$_]: [Getter/Setter], | |
input: [Getter/Setter], | |
[caller]: null, | |
lastMatch: [Getter/Setter], | |
$2: [Getter/Setter], | |
$5: [Getter/Setter], | |
$6: [Getter/Setter] | |
) | |
RegExp.prototype -> (Object | |
[toString]: (Function), | |
[constructor]: (Function...), | |
[exec]: (Function), | |
[compile]: (Function), | |
[test]: (Function) | |
) | |
example RegExp -> (RegExp | |
[lastIndex]: 0, | |
[multiline]: false, | |
[global]: true, | |
[source]: 'Find Me', | |
[ignoreCase]: true | |
) | |
Object -> (Function | |
[getOwnPropertyNames]: (Function), | |
[arguments]: null, | |
[seal]: (Function), | |
[length]: 1, | |
[create]: (Function), | |
[name]: 'Object', | |
[isFrozen]: (Function), | |
[keys]: (Function), | |
[prototype]: (), | |
[isExtensible]: (Function), | |
[getOwnPropertyDescriptor]: (Function), | |
[caller]: null, | |
[preventExtensions]: (Function), | |
[getPrototypeOf]: (Function), | |
[defineProperty]: (Function), | |
[isSealed]: (Function), | |
[defineProperties]: (Function), | |
[freeze]: (Function) | |
) | |
Object.prototype -> ( | |
[toString]: (Function), | |
[__lookupGetter__]: (Function), | |
[__defineGetter__]: (Function), | |
[toLocaleString]: (Function), | |
[hasOwnProperty]: (Function), | |
[valueOf]: (Function), | |
[__defineSetter__]: (Function), | |
[constructor]: (Function), | |
[propertyIsEnumerable]: (Function), | |
[isPrototypeOf]: (Function), | |
[__lookupSetter__]: (Function) | |
) | |
example Object -> (Object age: 28, name: 'Tim' ) | |
Array -> (Function | |
[arguments]: null, | |
[length]: 1, | |
[isArray]: (Function), | |
[name]: 'Array', | |
[prototype]: (Object), | |
[caller]: null | |
) | |
Array.prototype -> (Object | |
[length]: 0, | |
[constructor]: (Function), | |
[concat]: (Function), | |
[map]: (Function), | |
[sort]: (Function), | |
[join]: (Function), | |
[indexOf]: (Function), | |
[filter]: (Function), | |
[some]: (Function), | |
[toString]: (Function), | |
[reduceRight]: (Function), | |
[splice]: (Function), | |
[forEach]: (Function), | |
[shift]: (Function), | |
[unshift]: (Function), | |
[toLocaleString]: (Function), | |
[lastIndexOf]: (Function), | |
[reverse]: (Function), | |
[reduce]: (Function), | |
[pop]: (Function), | |
[push]: (Function), | |
[every]: (Function), | |
[slice]: (Function) | |
) | |
example Array -> (Array 1, 2, 3, 4, [length]: 4 ) | |
Buffer -> (Function | |
byteLength: (Function), | |
[arguments]: null, | |
isBuffer: (Function), | |
[length]: 3, | |
[name]: 'Buffer', | |
poolSize: 8192, | |
[prototype]: (Object...), | |
[caller]: null, | |
_charsWritten: 264 | |
) | |
Buffer.prototype -> (Object | |
binarySlice: (Function), | |
utf8Write: (Function), | |
utf8Slice: (Function), | |
toString: (Function), | |
inspect: (Function), | |
binaryWrite: (Function), | |
asciiSlice: (Function), | |
set: (Function), | |
copy: (Function), | |
write: (Function), | |
[constructor]: (Function...), | |
asciiWrite: (Function), | |
get: (Function), | |
slice: (Function) | |
) | |
example Buffer -> (Buffer 252, 131, 196, 4, length: 4, offset: 4048, parent: (SlowBuffer...) ) | |
EventEmitter -> (Function | |
[arguments]: null, | |
[length]: 0, | |
[name]: 'EventEmitter', | |
[prototype]: (Object...), | |
[caller]: null | |
) | |
EventEmitter.prototype -> (Object | |
on: (Function), | |
removeListener: (Function), | |
addListener: (Function), | |
once: (Function), | |
removeAllListeners: (Function), | |
emit: (Function), | |
[constructor]: (Function), | |
listeners: (Function) | |
) | |
example EventEmitter -> (EventEmitter | |
mainModule: (Module...), | |
createChildProcess: (Function), | |
setuid: (Function), | |
binding: (Function), | |
on: (Function), | |
cwd: (Function), | |
mixin: (Function), | |
stdout: [Getter], | |
unwatchFile: (Function), | |
_tickCallback: (Function), | |
title: 'node', | |
compile: (Function), | |
error: (Function), | |
platform: 'PLATFORM', | |
env: (...), | |
version: 'v0.3.2', | |
getgid: (Function), | |
chdir: (Function), | |
reallyExit: (Function), | |
setgid: (Function), | |
assert: (Function), | |
execPath: '/home/tim/.nvm/v0.3.2/bin/node', | |
exit: (Function), | |
kill: (Function), | |
pid: 9310, | |
argv: (Array...), | |
getuid: (Function), | |
debug: (Function), | |
memoryUsage: (Function), | |
ARGV: (Array...), | |
ENV: (...), | |
removeListener: (Function), | |
openStdin: (Function), | |
_needTickCallback: (Function), | |
installPrefix: '/home/tim/.nvm/v0.3.2', | |
nextTick: (Function), | |
versions: (Object...), | |
watchFile: (Function), | |
_kill: (Function), | |
EventEmitter: (Function), | |
dlopen: (Function), | |
addListener: (Function), | |
inherits: (Function), | |
_byteLength: (Function), | |
umask: (Function) | |
) | |
http -> (Object | |
STATUS_CODES: (Object...), | |
cat: (Function), | |
parsers: (...), | |
Server: (Function...), | |
ClientRequest: (Function...), | |
ServerResponse: (Function...), | |
createServer: (Function), | |
IncomingMessage: (Function...), | |
OutgoingMessage: (Function...), | |
createClient: (Function), | |
Client: (Function...) | |
) | |
net -> (Object | |
isIPv6: (Function), | |
Server: (Function...), | |
Stream: (Function...), | |
createConnection: (Function), | |
createServer: (Function), | |
isIPv4: (Function), | |
isIP: (Function) | |
) | |
url -> (Object | |
resolveObject: (Function), | |
resolve: (Function), | |
format: (Function), | |
parse: (Function) | |
) | |
path -> (Object | |
extname: (Function), | |
join: (Function), | |
normalizeArray: (Function), | |
split: (Function), | |
normalize: (Function), | |
basename: (Function), | |
existsSync: (Function), | |
exists: (Function), | |
dirname: (Function) | |
) | |
querystring -> (Object | |
decode: (Function), | |
escape: (Function), | |
stringify: (Function), | |
unescapeBuffer: (Function), | |
unescape: (Function), | |
encode: (Function), | |
parse: (Function) | |
) | |
child_process -> (Object spawn: (Function), execFile: (Function), exec: (Function) ) | |
step -> (Function | |
[arguments]: null, | |
fn: (Function), | |
[length]: 0, | |
[name]: 'Step', | |
[prototype]: (Object), | |
[caller]: null | |
) | |
connect -> (Object | |
middleware: (Object), | |
Server: (Function...), | |
createServer: (Function), | |
version: '0.5.0', | |
utils: (Object...) | |
) | |
haml -> (Function | |
render: (Function), | |
[arguments]: null, | |
[length]: 2, | |
[name]: 'Haml', | |
[prototype]: (Object), | |
execute: (Function), | |
[caller]: null, | |
optimize: (Function), | |
compile: (Function) | |
) | |
nstore -> (Object | |
remove: (Function), | |
genKey: (Function), | |
initialize: (Function), | |
save: (Function), | |
get: (Function), | |
[new]: (Function), | |
loadDatabase: (Function), | |
length: [Getter], | |
checkQueue: (Function) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment