Coder Social home page Coder Social logo

selfprint.js's Introduction

selfprint.js

A JavaScript program that prints itself

How it works

The idea is to implement a JS program that prints itself on runtime; console.log command will be used for output..

  1. Initially a program of such kind can have the following view: s = "<text>"; console.log('s = ' + '<"-symbol>' + s + '<"-symbol>' + s) The main trick is that <text> value includes everything after second double quotes symbol: ; console.log(... etc. We can't use double quotes in <text> value. If we try to escape those, it won't lead us to expected result: <text> string will include escaped values, but printed output wont't include characters used in escaping. So we can't use double quote charater at any position after enclosing quote of s="<text>" and inside <text> (which is the same sequence of characters).

  2. The answer is to use unicode escaped characters in strings, " - \u0022, ' - \u0027, \ - \u005c. This will help us to avoid issue related to nested quotes of the same type. But this don't solve the other problem - output of slash character.

  3. Let f() be the function that outputs any unicode character from given code. f('0022') -> ", f('005c') -> , etc. The f's body uses eval to process character code.

     f = function(a) {
         return eval('"\\u' + a + '"');
     };
    
  4. The program will have the following view: f = function(a) {...}; s = <text>; console.log('<f-output> s = ' + f('0022') + s + f('0022') + s').

     <f-output> = 'f = function(a) {return eval(' + f('0027') + f('0022') + f('005c') + f('005c') + 'u' + f('0027') + ' + a + ' + f('0027') + f('0022') + f('0027') + ');};'
    
  5. Finally, <text> value should be specified.

     <text> = '; console.log('f = function(a) {return eval(' + f('0027') + f('0022') + f('005c') + f('005c') + 'u' + f('0027') + ' + a + ' + f('0027') + f('0022') + f('0027') + ');}; s = ' + f('0022') + s + f('0022') + s);'
    
  6. Formated text of program. New line characters should be removed.

     f = function(a) {
         return eval('"\\u' + a + '"');
     };
     s = "; console.log('f = function(a) {return eval(' + f('0027') + f('0022') + f('005c') + f('005c') + 'u' + f('0027') + ' + a + ' + f('0027') + f('0022') + f('0027') + ');}; s = ' + f('0022') + s + f('0022') + s);";
     console.log('f = function(a) {return eval(' + f('0027') + f('0022') + f('005c') + f('005c') + 'u' + f('0027') + ' + a + ' + f('0027') + f('0022') + f('0027') + ');}; s = ' + f('0022') + s + f('0022') + s);
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.