Code convention in JavaScript

When I compress the javascript, I found that there are many problem in our codes.

Although I had pay some attention to the code problem, there still exist many problems. These problems are too subtile to absorb our attention.

for example, a function can be declared in two ways:

1.function test() {
      alert('this is a test');
}
 function test2() {
       alert('this is a test2');
}
 function test3() {
        alert('this is a test3');
}

2. var test = function() {
          alert('this is a test');
}
var test2 = function() {
        alert('this is a test2');
}
var test3 = function() {
     alert('this is a test3');
}

When to do the simple compression, we want to remove all the unnecessary chars from the codes. so all the lines of codes will be put together in one line. That makes problem.

In case 1, there will be no problem.However, thre are problems in case 2.
The script interpreter will says: missing ";" before .....
What's the problem?

if we just put the statement like this, then you will found the problem.

var test = 'aaa' var test2='bbb' var test3='ccc'

Got it. ";" (semicolon) need at the end of each statement.

The same reason for case 2.
The codes should be written like this:
var test = function() { alert('this is a test');}; var test2 = function() { alert('this is a test2');}; var test3 = function() { alert('this is a test3');};

The same rule for prototype statement:

String.prototype.trim = function() {
};
String.prototype.html = function() {
};

In one word, the ";" should not be missed at the end of Assignment statement.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值