Skip to content

Instantly share code, notes, and snippets.

View eligrey's full-sized avatar
:octocat:

Eli Grey eligrey

:octocat:
View GitHub Profile
jsandbox
.eval({
code : "x=1;Math.round(Math.pow(input, ++x))",
input : 36.565010597564445,
callback: function(n) {
console.log("number: ", n); // number: 1337
}
}).eval({
code : "][];.]\\ (*# ($(! ~",
onerror: function(ex) {
var foo = <></>; // XMLList literal
foo.push(<n>0</n>, 1, 5, 3, 2, 6, 4);
foo.toXMLString() === "<n>0</n><n>1</n><n>5</n><n>3</n><n>2</n><n>6</n><n>4</n>";
foo.sort(function(a, b) {
return a - b;
}).toXMLString() === "<n>0</n><n>1</n><n>2</n><n>3</n><n>4</n><n>5</n><n>6</n>";
foo.filter(function(x) { // filter out integers less then 3
if (!(x < 3))
return true;
}).toXMLString() === "<n>3</n><n>4</n><n>5</n><n>6</n>";
var results = [],
code = 'new Person("Elijah", "Grey")';
function Person(firstname, lastname) {
this.name = {
first: firstname,
last: lastname
};
}
results.push(code + " == " + eval(code));
function getClass(obj, useNative) {
return Object.prototype.toString.call(obj, useNative)
.match(/^\[object\s(.*)\]$/)[1];
}
function Foo(){}; // new foo == "[object Foo]"
var Bar = function(){}; // new Bar == "[object Object]"
Bar.name = "Foo"; // toStringX doesn't rely on function.name so this won't matter
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<!-- Online here: http://code.eligrey.com/testcases/all/isObjectLiteral.html -->
<title>isObjectLiteral</title>
<style type="text/css">
li { background: green; } li.FAIL { background: red; }
iframe { display: none; }
</style>
if (!Object.getPrototypeOf) {
if (typeof this.__proto__ === "object")
Object.getPrototypeOf = function (obj) {
return obj.__proto__;
};
else {
Object.getPrototypeOf = function (obj) {
var constructor = obj.constructor,
oldConstructor;
if (Object.prototype.hasOwnProperty.call(obj, "constructor")) {
@eligrey
eligrey / xml-send.js
Created August 21, 2009 21:10
xml.send()
XML.prototype.function::send = function (uri, callback) {
var prettyPrinting = XML.prettyPrinting,
req = new XMLHttpRequest();
req.open("POST", uri, false);
req.setRequestHeader("Content-Type", "application/xml");
if (typeof callback === "function") {
req.onreadystatechange = function () {
this.readyState === 4 && callback(this);
@eligrey
eligrey / gist:173011
Created August 22, 2009 22:09
JavaScript challenge: Fork this gist and make `mySolution' populate the `results' object with every formal parameter and their values passed to the function. (Yes, it's possible.) A formal parameter is the FOO in (function(FOO){}). To simplify the challe
(function () {
function mySolution ({
var,
this,
function,
if,
return,
true
}) {
// prohbit reference to arguments and the test object
// this example script uses a user-submitted script to modify image
// data from a canvas in which the user drew art
var sandbox = new JSandbox(),
userArt = document.getElementById("userArt").getContext("2d"),
imageData = userArt.getImageData(0, 0, userArt.width, userArt.height);
sandbox.load("user-submitted-script.js", function () { // onload
this.eval("doStuffWithImageData(input)", function (modifiedImageData) {
userArt.putImageData(modifiedImageData, 0, 0);