EN | RU
quine.js
meaning, briefly: define a variable with name — dollar sign ($
) and assign a function to it.
Give first argument of the function name — underscore sign (_
). The argument is not actually used inside the function, but it looks good in a quine. Make the function return a string with an expression inside (made possible by wrapping the string in backquotes (`
) and wrapping the expression in ${}
). The expression calls dollar sign ($
), which is the name of the function. Since we don't actually execute dollar sign ($
), this is not a recursive call. But when we execute the function, the expression gets replaced with the function we assigned to dollar sign ($
). After replacement, the string is returned from the function like about this — 1. beginning part of the string — ($=
, 2. our replaced expression — _=>`($=${$})()`
, 3. ending part of the string — )()
.
The following basically means the same as in quine.js
. Wouldn't be much of a quine nor a one-liner though :/
var $=function(){return `($=${$})()`};$()
The code can be wrapped for understanding as such:
(
$ =
_ =>
`($=${ $ })()`
)
()
-
(
with)
on lines 1 and 5 are just borders that wrap up a function, making it possible for()
on line 6 to "execute it". Such construction is called Immediately-Invoked Function Expression (IIFE). -
$ =
on line 2 means "define a variable with name $ that equals..." -
_ =>
on line 3 defines an Anonymous Arrow Function with first argument named_
and returning...
Since argument
_
is never called in the function and is not given any value when the function is called on line 6, it can be safely omited — replaced by()
— without hurting the quine logic.
`($=${ $ })()`
on line 4 is a Template Literal defined by covering anything in gravises, also known as backticks (`)
Template Literals (`smth`) allow to include an expression (like an argument's value) inside a String ("smth" or 'smth') by wrapping it in
${}
e.g. instead of
'a city called ' + city + ' is the capital of Great Britain'
we could write`a city called ${city} is the capital of Great Britain`
- Line 4 calls a variable named
$
, which takes us back to point 2, where we gave variable$
the following value:
_=>`($=${$})()`
- If we needed to represent line 4 as a String without calling any variables, we'd get the following string:
($=)()
. Now try copying the code block from point 5 and pasting it between($=
and)()
. You see it, right? :)
function dollar(_unused) {
return "($=" + dollar.toString() + ")()";
}
dollar();
(dollar=()=>'(dollar='+dollar+')()')()