zoukankan      html  css  js  c++  java
  • 格式化JavaScript代码

    javascript代码格式化工具

    网上下载的js代码经常遇到代码已被压缩(注释、换行、缩进、空格、TAB等都被删除了),如果拿来学习、研究的话必定看到头晕。有些编辑器的“格 式化代码”功能可以解决这个问题,在网上搜索了一下,找到一些这样的工具,其中用sourceformatx 很不错,支持很多语言,不过要收费,未注册的不能格式化大于8k的代码。注册费188元,太贵了;TextRush虽说免费,但好像根本没有发布; polystyle似乎不错;vs.net的插件ReSharper也有这样的功能,这类工具好像还有:Trita、Eclipse等,不过最后找到了 Javascript Code Improver, 一个免费的工具。他很简单,只有这一个功能,就是格式化js,也可以格式化和html混排代码,足够用了。
            效果:

    <script language="JavaScript">var i=0,s="",k=0;function foo(){for(j=0;j<10;j++){for(i=0;i<10;i++){s="string1";k=Math.floor(Math.random()*10);}for(i=20;i>9;i--){s="string2";k=i;}}}</script>

    格式化后:

    <script language="JavaScript">
    var i = 0, s = "", k = 0;
    function foo()
    {
        for(j = 0; j < 10; j++)
        {
            for(i = 0; i < 10; i++)
            {
                s = "string1";
                k = Math.floor( Math.random()*10 );
            }
            for(i = 20; i > 9; i--)
            {
                s = "string2";
                k = i;
            }
        }
    }
    </script>

    另外如果要对js代码进行压缩的话,可以用这个:http://www.brainjar.com/js/crunch/demo.html  

    使用该工具,要注意必须保证每个语句后面都有分号 (';'),不然会报类似 "expected ';'" 或 "missing ; before statement" 这样的错误。比如
    x = 3.141592
    y = 2.718281

    会变成 x=3.141592 y=2.718281,是个错误的语句。不过也有个好处,可以检查出你哪些地方漏写了分号。比较容易不写分号的情况是:p.onreadystatechange = function(){....};rssInfos[i] = { id: idStr, url: urlStr }; 这两种。

    From: http://blog.donews.com/bigcarp/archive/2006/01/12/691890.aspx

    补充:

    JavaScript Code Improver

    JavaScript Code Improver is an easy-to-use utility that makes the JavaScript code in your Web pages well-structured and easily comprehensible thus saving the time you spend on editing, debugging and modifying it. The program features the multilingual interface and support for drag-n-drop operations, and can handle JavaScripts embedded in the Web pages as well as stored in standalone .js files.

    网址: http://jcay.com/id-190119110113039.html     下载:http://jcay.com/jscisetup.exe

    1.javascript格式化及压缩工具 可以格式化被压缩或者没有格式的javascript代码

    可以压缩javascript代码  

    网址:http://www.div-css.com/f/tool/js-format-compress.html



  • 相关阅读:
    uva 11294 Wedding
    uvalive 4452 The Ministers’ Major Mess
    uvalive 3211 Now Or Later
    uvalive 3713 Astronauts
    uvalive 4288 Cat Vs. Dog
    uvalive 3276 The Great Wall Game
    uva 1411 Ants
    uva 11383 Golden Tiger Claw
    uva 11419 SAM I AM
    uvalive 3415 Guardian Of Decency
  • 原文地址:https://www.cnblogs.com/emanlee/p/1269908.html
Copyright © 2011-2022 走看看