[关闭]
@kungfuboy 2016-12-30T03:49:53.000000Z 字数 2018 阅读 1065

前端代码质量

未分类


try...catch...

  1. // 奇酷消息盒子航班页面——flight.js
  2. onload = function() {
  3. ···
  4. try {
  5. if (flight_num == "null" || flight_num == "NULL") {
  6. flight_num = flight_numArr[1].replace(" ", "").replace(" ", "");
  7. } else {
  8. flight_num = flight_numArr[0].replace(" ", "").replace(" ", "");
  9. }
  10. } catch (e) {
  11. // TODO: handle exception
  12. }
  13. ···
  14. try {
  15. flight_from_time = window.injs.getConfigByKey('flight_time');
  16. flight_to_time = window.injs.getConfigByKey('arrive_time');
  17. flight_to_date = window.injs.getConfigByKey('arrive_date');
  18. } catch (e) {
  19. // TODO: handle exception
  20. }
  21. ···
  22. }

第14行和第25行,catch中并没有代码,依旧保留着try

  1. //850ms
  2. !function() {
  3. //无 try catch 的情况耗时
  4. var t = new Date();
  5. //耗时代码开始
  6. for (var i = 0; i < 100000000; i++) {
  7. var p = i % 2;
  8. }
  9. //耗时代码结束
  10. document.write(new Date() - t);
  11. try{
  12. }catch(e){
  13. }
  14. }();
  15. //140ms
  16. !function() {
  17. //无 try catch 的情况耗时
  18. var t = new Date();
  19. //耗时代码开始
  20. for (var i = 0; i < 100000000; i++) {
  21. var p = i % 2;
  22. }
  23. //耗时代码结束
  24. document.write(new Date() - t);
  25. }();
  26. //修改后
  27. !function() {
  28. !function() {
  29. //无 try catch 的情况耗时
  30. var t = new Date();
  31. //耗时代码开始
  32. for (var i = 0; i < 100000000; i++) {
  33. var p = i % 2;
  34. }
  35. //耗时代码结束
  36. document.write(new Date() - t);
  37. }();
  38. try{
  39. }catch(e){
  40. }
  41. }();

空判断

  1. function isNull(data){
  2. return (data == "" || data == undefined || data == null || data == 0) ? true : false;
  3. }
  4. ···
  5. if(!isNUll(data)) {
  6. ···
  7. }

其实,这样就可以了:

  1. !!data

PS: 字符串中的0无法判断

面向对象

面向全局写法:

  1. window.onload = {
  2. init();
  3. }
  4. function init() {
  5. methon1();
  6. methon2();
  7. methon3();
  8. }
  9. function methon1(){}
  10. function methon2(){}
  11. function methon3(){}

普通的面向对象写法:

  1. function App() {}
  2. App.prototype.init = function() {}
  3. App.prototype.render = function() {}
  4. var app = new App();
  5. app.init();
  6. app.render();

从jQuery里面学习到的写法:

  1. window.onload = function(){
  2. App();
  3. }
  4. function App() {
  5. return new App.prototype.init();
  6. }
  7. App.prototype.init = function() {}
  8. App.prototype.init.prototype = App.prototype;

修改样式

  1. //jquery
  2. $(el).css("width");
  3. //普通的js
  4. getElementById('el').style.width;
  5. //js
  6. getComputedStyle(el)["width"];

监听和数据劫持

  1. //流量充值页面
  2. if ($("#phone").length > 0) {
  3. $("#phone").bind("keyup", function(e) {
  4. var t = this.value.length;
  5. if(phonenum == $("#phone").val()){
  6. return ;
  7. }
  8. Object.defineProperty(obj, key, {
  9. enumerable: true, //属性是否可枚举
  10. configurable: true, //属性是否可配置
  11. get: function() {
  12. },
  13. set: function() {
  14. }
  15. });

https://github.com/search?p=1&q=stars%3A%3E1&s=stars&type=Repositories

https://github.com/RubyLouvre/avalon

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注