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. ArcSDE空间数据库中SDE用户使用探讨(转)

    ArcSDE作为空间数据库解决方案,应用非常广泛,本短文将尝试描述SDE的工作机制,简要说明空间数据 库中SDE用户的使用方法. ArcSDE如何工作 ArcSDE属于中间件技术,其本身并不能够存储空 ...

  2. Android setUserVisibleHint-- fragment真正的onResume和onPause方法

    这个情况仅适合与多个fragment之间切换时统计,而非activity和fragment同时交互,因当时项目为首页4个fargment时长统计,因此适合,经下面网友评论指出,特在这里写出此问题,因最 ...

  3. 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser

    Intents 这个例子的代码非常简单: public void onGetMusic(View view) { Intent intent = new Intent(Intent.ACTION_GE ...

  4. Laravel Scheduling Package

    Laravel 是在 App\Console\Kernel 类中的 schedule 方法去定义所有的调度任务. iBrand 产品作为一个电商类产品,本身业务需求非常庞大和复杂,全部定义在 sche ...

  5. 浏览器中使用calc不识别

    在使用css3中的calc运算函数时,发现浏览器不识别,当时代码是这样的 width:calc(100%-50px); 经过查询官网原来发现这里有个需要注意的地方就是在进行加减运算的时候,必须在运算符 ...

  6. Installing xgboost and cmake, mingw64 and mingw

    Problem: installing the xgboost to get the python package for later importing

  7. 转载:手把手教你搭建 vue 环境

    以下内容转自: https://segmentfault.com/a/1190000008922234 第一步 node环境安装 1.1 如果本机没有安装node运行环境,请下载node 安装包进行安 ...

  8. 【LOJ115】无源汇有上下界可行流(模板题)

    点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...

  9. 【BZOJ4025】二分图(LCT动态维护图连通性)

    点此看题面 大致题意: 给你一张图以及每条边的出现时间和消失时间,让你求每个时间段这张图是否是二分图. 二分图性质 二分图有一个比较简单的性质,即二分图中不存在奇环. 于是题目就变成了:让你求每个时间 ...

  10. Uva 11401 数三角形

    题目链接:https://uva.onlinejudge.org/external/114/11401.pdf 题意:1~n个数里面挑3个不同的数,组成一个三角形.求方案数. 分析: 令最长的边为X, ...