如何添加换行到quine函数,我想要适当的缩进和每行源代码后的换行

How to add line breaks to quine function,I want proper indentation and line breaks after each line of source code

本文关键字:换行 缩进 源代码 我想要 何添加 添加 函数 quine      更新时间:2023-09-26

这里我使用一个函数来显示另一个函数的源代码,但源代码没有正确缩进,也没有换行。如果我在控制台中打印相同的代码,它会正确缩进。我只是想使源代码正确缩进与空白和换行符。我不知道如何做到这一点。虽然我试过模式匹配,但运气不好。提前感谢

function calc(){
    var x,y,x;
     x = 10;
     y = 56;
     z = x + y;
     console.log('The sum is: ' + z);
  }
function showCode(){
  var target = document.getElementById("writeCode");
  var sourceCode;
  sourceCode = String(calc);
  target.innerHTML = sourceCode; //Not indented and no white spaces
  console.log(String(calc)); //Everything is fine
}
<button onclick = "showCode()">SHOW SOURCE</button>  
<p id = "writeCode"></p> 

试试<pre id = "writeCode"></pre>,在这种情况下,它可能是最正确的标签。

标签定义了预格式化的文本。

元素中的文本以固定宽度的字体(通常是Courier)显示,并且保留空格和换行符。

在w3schools阅读更多

只要使用pre标签就可以在html

中呈现

function calc(){
    var x,y,x;
     x = 10;
     y = 56;
     z = x + y;
     console.log('The sum is: ' + z);
  }
function showCode(){
  var target = document.getElementById("writeCode");
  var sourceCode;
  sourceCode = String(calc);
  target.innerHTML = sourceCode;
}
<button onclick = "showCode()">SHOW SOURCE</button>  
<pre id = "writeCode"></pre>