Skip to content

Instantly share code, notes, and snippets.

@moshest
Last active September 7, 2017 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moshest/2f9741e9b00eb0833d44f915ab5bd1d5 to your computer and use it in GitHub Desktop.
Save moshest/2f9741e9b00eb0833d44f915ab5bd1d5 to your computer and use it in GitHub Desktop.
A JavaScript function that reproduce itself as a Quine program
/**
* A JavaScript function that reproduce itself as a Quine program.
* Copyright (c) 2017 Moshe Simantov
*/
const assert = require('assert');
const quine = (function () {
var f = '(function () {%0A var f = %27@%27;%0A%0A return eval(unescape(f).replace(/@/, f));%0A})';
return eval(unescape(f).replace(/@/, f));
});
const quineCode = quine.toString();
assert.equal(quineCode, quine().toString());
assert.equal(quineCode, quine()().toString());
assert.equal(quineCode, quine()()().toString());
console.log(`const quine = (${quineCode});`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment