【问题标题】:Which is the best way to handle long write() arguments?处理长 write() 参数的最佳方法是什么?
【发布时间】:2008-10-27 02:27:52
【问题描述】:

更新:感谢大家的回复。我没有意识到 document.write() 已被弃用。在学习栏上再添一个档次。我将接受此处发布的建议,但保留原始问题,以便给出的答案在原始问题的上下文中有意义。


我正在编写一些相当长的 write() 参数,并考虑到语法、可读性和性能,尝试确定以下哪个示例最适合遵循。我应该

一个。将它们全部放在一行上:

<script>

    var someVariable = "(<a href=\"http://www.example.com\">Link<\/a>)";

    document.write("<p>Supergroovalisticprosifunkstication and Supercalifragilisticexpialidocious are very long words.</p>" + someVariable + "<p>Dociousaliexpisticfragilicalirepus is Supercalifragilisticexpialidocious spelled backwards.</p>" + someVariable);

</script>

b.通过添加换行符来拆分它们以提高可读性:

<script>

    var someVariable = "(<a href=\"http://www.example.com\">Link<\/a>)";

    document.write("<p>Supergroovalisticprosifunkstication and Supercalifragilisticexpialidocious are very long words.</p>" 
        + someVariable
        + "<p>Dociousaliexpisticfragilicalirepus is Supercalifragilisticexpialidocious spelled backwards.</p>" 
        + someVariable);

</script>

c。使用多个变量将它们分解:

<script>

    var someVariable = "(<a href=\"http://www.example.com\">Link<\/a>)";

    var partOne = "<p>Supergroovalisticprosifunkstication and Supercalifragilisticexpialidocious are very long words.</p>"; 
    var partTwo = "<p>Dociousaliexpisticfragilicalirepus is Supercalifragilisticexpialidocious spelled backwards.</p>"; 

    document.write(partOne + someVariable + partTwo + someVariable);

</script>

提前致谢。

【问题讨论】:

  • @CodeCurious - 不要删除原始问题,因为这样很难理解答案的内容。该网站旨在对追随您的许多人有用,但如果没有原始信息,就很难知道这是关于什么的。

标签: javascript readability


【解决方案1】:

我的直觉反应是:不要那样做。 (你的例子很糟糕,你不应该在你的行为层写大块的内容。)

每当您必须这样做时,要么连接:

var longVar = 'asdfasdf asdf asdf asdfasdf asdfasdf asdf asdfasdf' +
    ' fasdf s9d0af asdf asdf0s,dv z-xcfva-sdfmwaert ' +
    'qersdfasdfasdfasdfasdf';
document.write(longVar);

或者如果它变得很长,使用加入数组可能会提高性能:

var longVar = [
    'asdfasdf asdf asdf asdfasdf asdfasdf asdf asdfasdf',
    ' fasdf s9d0af asdf asdf0s,dv z-xcfva-sdfmwaert ',
    'qersdfasdfasdfasdfasdf'
].join('');
document.write(longVar);

【讨论】:

    【解决方案2】:

    我会写它,但它最容易阅读和维护。然后测试性能。如果速度太慢,请尝试逐步改进算法,直到速度可以接受。

    所以提高性能的想法: - 确保脚本被缩小。 - 在服务器上进行尽可能多的预处理并提供“已处理”脚本。

    通过使用缩小工具(例如 jsMin),您不会因为有空格和换行符以提高可读性而遇到任何问题。

    【讨论】:

      【解决方案3】:

      -- 使用 document.write() 是一个不好的例子,因为它属于 90 年代,并且随着 1998 年 HTML4 的引入而被弃用...

      如果你有任何服务器端的东西,最好在那里处理代码生成......

      关于连接字符串的问题,我同意没有眼睑!-)

      编辑 (29/10)

      作为对评论的评论(我需要代码符号)

      <script type="text/javascript">
        window.onload = function(){
          var newD = document.createElement("div");
          newD.appendChild(document.createTextNode("Hello World"));
          document.getElementsByTagName("body")[0].appendChild(newD);
        }
      </script>
      

      这样任何东西都可以插入到文档中...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 2012-09-18
        • 2011-05-20
        • 2015-07-05
        • 2016-05-26
        相关资源
        最近更新 更多