Some items you should konw :
  • Graceful degradation : ensuring that your web pages still work without JavaScript
  • Unobtrusive JavaScript : separating structure from behavior
  • Backward compatibility : ensuring that older broswers dont choke on your scirpts
  • Performance consideration : making sure that your script is performing at its best
 
 
Graceful degradation
Its always worth remembering that a visitor to your site might be using a browser that doesnt
support JavaScript. Or maybe the user has disabled JavaScript. If you dont consider this possibility,
you could inadvertently stop visitors from using your site. 
 
 
Unobtrusive JavaScript
Using an attribute like onclick in the markup is just as inefficient as using the style attribute.
You can attach an event to an element in an external JavaScript file :
        element.event = action ... 
eg : if you want to attach an event to an element with a unique ID, you can simply use getElementByID :
        getElementById(id).event = action  
There is just one problem. This code will be executed as soon as the JavaScript file loads.
But there is no guarantee which files will finish loading first. So the document may be incomplete when 
the script loads, the model of the document is also incomplete, and methods like getElementByTagName
simply wont work.
You need to execute the code once you're sure the document has finished loading.
Fortunately, the complete loading of a document is an event with a corresponding event handler.
        window.onload = prepareLinks ; 
        function prepareLinks() { ... }
 
Backward compatibility 
If you wrap a method in an if statement, the statement will evaluate to either true or false, depending on 
whether the method exists . This is called object detection.
As you saw before, methods are objects, just like almost everything else in JavaScript. It makes it quite esay
to exclude browsers that dont support a specific DOM method.
        if( !getElementById || !getElementByTagName ) return false;
 
 
Performance consideration
  • Minimizing DOM access and markup ( 减少DOM 以及 标记 )
  • Assembling and placing scripts ( 合并放置 脚本 ) :
     Reducing the number of requests required to load a page is probably the number one thing you can do 
     to improve the overall load time of your website.
  • Minification : this refers to the process of taking your script and "compressing" it by removing the unnecessary bits such as whitspace and comments.  Minifed code isnt pretty or human-readable, but it can make a big difference in filse size.
      In most cases, you will need to keep two copies : a working copy, in which you can make changes an 
      commens, and the minified copy, which you serve up on your site. As a standard convention, a good idea it
      to include min in the file name of minified files to distinguish them from their nonminified couterparts.
              <script src = "sciptes/scriptName.min.js"> </scirpt>
 

Best Practices in JavaScript的更多相关文章

  1. web前端性能意义、关注重点、测试方案、优化技巧

    1.前段性能的意义 对于访问一个网站,最花费时间的并不是后端应用程序处理以及数据库等消耗的时间,而是前端花费的时间(包括请求.网络传输.页面加载.渲染等).根据web优化的黄金法则: 80%的最终用户 ...

  2. Cheatsheet: 2019 07.01 ~ 09.30

    Other Intro Guide to Dockerfile Best Practices QuickJS Javascript Engine Questions for a new technol ...

  3. 45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)

    As you know, JavaScript is the number one programming language in the world, the language of the web ...

  4. 转:45 Useful JavaScript Tips, Tricks and Best Practices

    原文来自于:http://flippinawesome.org/2013/12/23/45-useful-javascript-tips-tricks-and-best-practices/ 1 – ...

  5. JavaScript Best Practices (w3cschool)

    JavaScript Best Practices (w3cschool) Local Variables: ·      总是在前面集中定义变量,(包括 for 的i).(strict mode) ...

  6. 45 Useful JavaScript Tips, Tricks and Best Practices

    <45 Useful JavaScript Tips, Tricks and Best Practices> http://flippinawesome.org/2013/12/23/45 ...

  7. JavaScript best practices JS最佳实践

    JavaScript best practices JS最佳实践 0 简介 最佳实践起初比较棘手,但最终会让你发现这是非常明智之举. 1.合理命名方法及变量名,简洁且可读 var someItem = ...

  8. 《javascript高级程序设计》 第24章 最佳实践 Best Practices

    24.1 可维护性 Maintainability24.1.1 什么是可维护的代码 What Is Maintainable Code?24.1.2 代码约定 Code Conventions 24. ...

  9. 24 javascript best practices for beginner(only 23 finally)

    原文是英文,链接: http://net.tutsplus.com/tutorials/JavaScript-ajax/24-JavaScript-best-practices-for-beginne ...

随机推荐

  1. java之操作mysql常用方法

    一般引用mysql-connector-java这个包. package DBManager; import java.sql.Connection; import java.sql.DriverMa ...

  2. VC++ MFC类库基础(55讲全)

    视频保存在播音员 网盘中内容简介: 本部分是您成为VC++软件工程师必备的阶段,如果您没有任何基础,学习C++能快速让您进入编程领域,建议配合书籍<C++入门经典> 关键词: VC++.V ...

  3. 2017.10.28 QB模拟赛 —— 下午

    题目链接 T1 按x值排序 遇到第二种牌插入 遇到第一种牌 查询<=y 的最小值 删除他 splay multiset cys大佬说 multiset就是不去重的set, #include &l ...

  4. Educational Codeforces Round 14 - F (codeforces 691F)

    题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...

  5. MHA 日常维护命令集

    MHA 日常维护命令集 1.查看ssh登陆是否成功 masterha_check_ssh --global_conf=/etc/masterha/masterha_default.conf --con ...

  6. user(),current_user()函数的区别

    user() 表示当前的登录用户   current_user() 表示对应于mysql.user表里对应的账号.

  7. swift 注解 (和java比照)@attribute name

    Attributes provide more information about a declaration or type. There are two kinds of attributes i ...

  8. 【BZOJ5212】[ZJOI2018] 历史(LCT大黑题)

    点此看题面 大致题意: 给定一棵树每个节点\(Access\)的次数,求最大虚实链切换次数,带修改. 什么是\(Access\)? 推荐你先去学一学\(LCT\)吧. 初始化(不带修改的做法) 首先考 ...

  9. ZOJ 2314 Reactor Cooling 带上下界的网络流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的, ...

  10. google detection

    paper: Scalable, High-Quality Object Detection ILSVRC14上,detection刷到55.7%的MAP,google插入的地方,别人倍感压力啊. 总 ...