简体   繁体   English

如何缩短javascript中的行数?

[英]How can I shorten the lines in javascript?

I've got the google analytics javascript and I want to make it smaller. 我有Google Analytics(分析)JavaScript,并且希望将其缩小。 But I thought that you couldn't just put an enter somewhere... So where CAN I start a new line in this code? 但是我认为您不能只是在某个地方输入一个内容...那么我在哪里可以在这段代码中开始新的一行?

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'secret']); _gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

EDIT: Why? 编辑:为什么? Because my screen is to small. 因为我的屏幕很小。 It's for readability. 这是为了提高可读性。

EDIT2: What about this approach? EDIT2:这种方法呢? (The use of a '\\') (使用“ \\”)

 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : '\
 http://www') + '.google-analytics.com/ga.js';

I'm really not sure why you want to do this, nor would I recommend it, but here you go. 我真的不确定为什么要这么做,也不会推荐,但是现在就开始吧。

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'secret']);
_gaq.push(['_trackPageview']);
(function() {
  var ga = document.createElement('script');
  ga.type = 'text/javascript'; ga.async = true;
  var start;
  if ('https:' == document.location.protocol) {
    start = 'https://ssl';
  } else {
    start = 'http://www';
  }
  ga.src = start + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(ga, s);
})();

尝试在线YUI压缩程序,它适用于javascript和CSS。

You can start a new line after each semi-colon. 您可以在每个分号之后开始新的一行

Before: 之前:

var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

After: 后:

var ga = document.createElement('script'); 
ga.type = 'text/javascript'; 
ga.async = true;

To my eye, this makes it much easier to read. 在我看来,这使阅读起来更加容易。 I wouldn't necessarily keep it this way when I deploy it, but you could. 部署它时,我不一定会保持这种方式,但是可以。

In addition to being easier to read, this makes it easier to step through line by line if you are debugging. 除了易于阅读之外,如果您正在调试,这将使逐行浏览变得更容易。

after any ';' 在任何';'之后

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'secret']);
_gaq.push(['_trackPageview']);
(function() {
    var ga = document.createElement('script'); 
    ga.type = 'text/javascript'; 
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(ga, s);
})();

But why do you want to do that? 但是为什么要这么做呢?

You can do it manually after every semicolon, but you can also do it automatically. 您可以在每个分号后手动执行此操作,但也可以自动执行。 Try entering "javascript formatter" into Google and you get Online JavaScript Beautifier for example. 尝试在Google中输入“ javascript格式化程序”,例如,您将获得Online JavaScript Beautifier

This is the code after "beautification": 这是“美化”之后的代码:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'secret']);
_gaq.push(['_trackPageview']);
(function () {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM