Skip to content

Instantly share code, notes, and snippets.

@tobie
Created March 18, 2009 13:35
Show Gist options
  • Save tobie/81118 to your computer and use it in GitHub Desktop.
Save tobie/81118 to your computer and use it in GitHub Desktop.
SpiderMonkey's uneval for common types
function repr(v) {
print(uneval(v))
}
repr(false)
//-> false
repr(true)
//-> true
repr(null)
//-> null
repr(undefined)
//-> (void 0)
repr('foo')
//-> "foo"
repr(123)
//-> 123
repr(123.4)
//-> 123.4
r = new RegExp('foo')
repr(r)
//-> /foo/
repr(new Date)
//-> (new Date(1237141453430))
repr([0, 1, [2, 3]])
//-> [0, 1, [2, 3]]
repr({foo: 123})
//-> ({foo:123})
a = [1, 2, 3]
a.push(a)
repr(a)
//-> #1=[1, 2, 3, #1#]
h = {foo: 123}
h.bar = h
repr(h)
//-> #1={foo:123, bar:#1#}
function Foo() {
var self = {}
self.__init__ = function () {
self.foo = 123
self.bar = 456
}
return self
}
repr(Foo())
//-> ({__init__:(function () {self.foo = 123;self.bar = 456;})})
repr(Foo)
//-> function Foo() {var self = {};self.__init__ = function () {self.foo = 123;self.bar = 456;};return self;}
repr([].reverse)
//-> function reverse() {[native code]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment