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 Knowledge series 1

    Programming language evolves always along with Compiler's evolvement JVM as Additional Indirection I ...

  2. 【起航计划 026】2015 起航计划 Android APIDemo的魔鬼步伐 25 App->Notification->Status Bar 状态栏显示自定义的通知布局,省却声音、震动

    这个例子的Icons Only 和 Icons and marquee 没有什么特别好说明的. 而Use Remote views in balloon 介绍了可以自定义在Extended Statu ...

  3. SAP UI5和CRM WebUI的View和Controller是如何绑定的

    UI5 例如我在UI5的界面上画一个按钮,点击之后弹出一个Alert dialog. 在XML view里只定义了controller的名称和事件处理函数的名称.那么按钮被点击之后,controlle ...

  4. Android(java)学习笔记141:Android下的逐帧动画(Drawable Animation)

    1. 帧动画: 帧动画顾名思义,一帧一帧播放的动画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一 ...

  5. Android(java)学习笔记60:继承中父类 没有无参构造

    1. 继承中父类 没有无参构造: package com.himi.test1; /* 如果父类没有无参构造方法,那么子类的构造方法会出现什么现象呢? 报错. 如何解决呢? A:在父类中加一个无参构造 ...

  6. 字符串反转,栈模拟(ZOJ1151)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 这里可以用栈模拟,也可以用STL,reverse();函数. 但 ...

  7. centos6.5下编译安装FFmpeg

    以下安装步骤基本来自官网,做个笔记以方便自己以后查看 http://trac.ffmpeg.org/wiki/CompilationGuide 1.安装依赖包 <span style=" ...

  8. 调整JVM占用内存空间方法

    JVM默认占用空间为64M 调整方法如下图 在虚拟机参数中调整为80M 调试可以用 Byte[] arr=new Byte[1024*1024*64];

  9. TSMessages,非HUD风格的iOS提示框(附官方demo BUG修复方案)

    优势 先看效果 个人觉得这种提示效果用在UITableView上要比HUD优雅美观,而其他情况下的提示,用HUD比较好 源码简介易懂,用起来也很方便 导入 pod导入相对很简单,主要讲怎么手动导入这个 ...

  10. LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal

    题目 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3 ...