Best Practices in JavaScript

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>

时间: 2024-09-30 04:14:52

Best Practices in JavaScript的相关文章

JavaScript best practices

Summary This article discusses some best practices to follow for writing efficient, understandable JavaScript. Introduction Writing a best practice article is quite a tricky business. To a number of you, what you are about to read will appear to be v

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, of mobile hybrid apps (like PhoneGap or Appcelerator), of the server side (like NodeJS or Wakanda) and has many other implementations. It’s also the

45个实用的JavaScript技巧、窍门和最佳实践

如你所知,JavaScript是世界上第一的编程语言,它是Web的语言,是移动混合应用(mobile hybrid apps)的语言(比如PhoneGap或者Appcelerator),是服务器端的语言(比如NodeJS或者Wakanda),并且拥有很多其他的实现.同时它也是很多新手的启蒙语言,因为它不但可以在浏览器上显示一个简单的alert信息,而且还可以用来控制一个机器人(使用nodebot,或者nodruino).掌握JavaScript并且能够写出组织规范并性能高效的代码的开发人员,已经

45种Javascript技巧大全

原文:45 Useful JavaScript Tips, Tricks and Best Practices 译文:45个有用的JavaScript技巧,窍门和最佳实践 译者:dwqs 在这篇文章中,我将分享一些JavaScript常用的技巧,窍门和最佳实践.不管JavaScript开发者是使用在浏览器/引擎上或者服务器端(SSJS--Service Side JavaScript)JavaScript解释器上,这些他们都是应该知晓的. 需要注意的是,文章中的代码片段均是在最新的Google

The Dangers of JavaScript’s Automatic Semicolon Insertion

Although JavaScript is very powerful, the language’s fundamentals do not have a very steep learning curve.  Prior to the explosion of web applications, JavaScript was thought of as a toy language for amateur programmers.  Some of JavaScript’s feature

JavaScript Modules

One of the first challenges developers new to JavaScript who are building large applications will have to face is how to go about organizing their code. Most start by embedding hundreds of lines of code between a <script> tag which works but quickly

Javascript Tips &amp; Tricks

前端开发规范系列文章之Javascript Tips and Tricks,本意是写成常用代码收集.常用技巧整理的文章,感觉"常用代码大全"太土."实用代码整理"有失偏颇,"提示与技巧"不够稳重,所以使用常用的英语说法,相信广大程序员都懂得. 妙味 Javascript美妙之处,需要我们静静体会,慢慢吸收,然后让代码在您指下曼舞.整理的这些代码我们称之为妙味,请大家细品. 博主会不断更新本文,方便大家阅读起见,我们采用倒序更新的方式,把最新更新的

Best Practices for Speeding Up Your Web Site

The Exceptional Performance team has identified a number of best practices for making web pages fast. The list includes 35 best practices divided into 7 categories. Looking to optimize your mobile app experience? Check out Flurry Analytics. Filter by

10+ Useful Javascript Libraries for Your New Proje

JavaScript Library is basically a pre-written scripting language that ease the development of JavaScript based applications such as AJAX and other web-centric technologies.These JavaScript libraries are often called JavaScript frameworks.Popular Java