原文: https://www.w3schools.com/js/js_best_practices.asp

----------------------------------------------

Avoid global variables,  avoid new,  avoid  ==,  avoid eval()


Avoid Global Variables

Minimize the use of global variables.

This includes all data types, objects, and functions.

Global variables and functions can be overwritten by other scripts.

Use local variables instead, and learn how to use closures.


Always Declare Local Variables

All variables used in a function should be declared as local variables.

Local variables must be declared with the var keyword, otherwise they will become global variables.

Strict mode does not allow undeclared variables.


Declarations on Top

It is a good coding practice to put all declarations at the top of each script or function.

This will:

  • Give cleaner code
  • Provide a single place to look for local variables
  • Make it easier to avoid unwanted (implied) global variables
  • Reduce the possibility of unwanted re-declarations
// Declare at the beginning
var firstName, lastName, price, discount, fullPrice;

// Use later
firstName = "John";
lastName = "Doe";

price = 19.90;
discount = 0.10;

fullPrice = price * 100 / discount;

This also goes for loop variables:

// Declare at the beginning
var i;

// Use later
for (i = 0; i < 5; i++) {

JavaScript Best Practices相关推荐

  1. JavaScript 初学者应知的 24 条最佳实践

    原文:24 JavaScript Best Practices for Beginners (注:阅读原文的时候没有注意发布日期,觉得不错就翻译了,翻译到 JSON.parse 那一节觉得有点不对路才 ...

  2. 给javascript初学者的24条最佳实践

    1.使用 === 代替 == JavaScript 使用2种不同的等值运算符:===|!== 和 ==|!=,在比较操作中使用前者是最佳实践. "如果两边的操作数具有相同的类型和值,===返 ...

  3. javascript错误_JavaScript开发人员最常犯的10个错误

    javascript错误 常见错误1:对" this'错误引用 (Common Mistake #1: Incorrect references to 'this') As JavaScri ...

  4. JavaScript2谁刚开始学习应该知道4最佳实践文章(翻译)

    原版的:24 JavaScript Best Practices for Beginners (注:阅读原文的时候没有注意公布日期,觉得不错就翻译了,翻译到JSON.parse那一节觉得有点不正确路才 ...

  5. 消除拖延的方法_拖延很烂—因此,这就是“吃青蛙”提高生产力的方法

    消除拖延的方法 在最有价值的活动上花费最宝贵的时间 ,您将改变自己的生活轨迹. (Spend Your Most Valuable Time on Your Most Valuable Activit ...

  6. [乐意黎转载]高效 jquery 的奥秘

    原文:Writing Better jQuery Code 作者:@MathewDurden 译文:高效 jquery 的奥秘 译者:@颜海镜 讨论 jQuery 和 javascript 性能的文章 ...

  7. 面向对象的前端开发_20多种面向前端开发人员的文档和指南(第9号)

    面向对象的前端开发 It's that time again to get learning! As before, I've collected a number of different lear ...

  8. dropzonejs vue 使用_dropzone.js使用实践

    官网地址:http://www.dropzonejs.com/ 一,它是什么: DropzoneJS is an open source library that provides drag'n'dr ...

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

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

最新文章

  1. 学习 Linux,101: 引导系统
  2. 获取磁盘空间大小计算成看k,m,G
  3. c语言运行时隐藏dos窗口
  4. EasyUI中Datagride数据网格的简单使用
  5. 最长上升子序列(LIS)长度
  6. 关于Linux的虚拟内存管理
  7. 多角度让你彻底明白yield语法糖的用法和原理及在C#函数式编程中的作用
  8. bash: pcre-config: 未找到命令..._Docker 常用操作命令
  9. Fedora中systemctl命令的使用
  10. 开源数据库:何为NoSQL生态系统?
  11. 前端怎么自我介绍_实习|猫眼前端面试经历,问问题很关键(已拿offer)
  12. 本地通过Eclipse链接Hadoop操作Mysql数据库问题小结
  13. cad线性标注命令_CAD线性标注快捷键是什么,怎么使用
  14. jpg怎么合成一份_怎么将多个jpg合并成一个pdf
  15. 计算机向文档中插入文本框,Word怎么插入文本框和编辑文本框
  16. 人类无法抗拒的十大心理学效应
  17. 贪心法和动态规划法的区别
  18. 利用stylecloud制作更酷炫的词云图
  19. Linux驱动开发(一)
  20. 用html画布做扇形,jquery canvas 画扇形

热门文章

  1. 实用 | 如何利用 Burp Suite 进行密码爆破
  2. 青藤正式加入微软MAPP计划
  3. 颤抖吧!00后已经开始爆肝写游戏赚钱了!
  4. 安装DirectShow的Filter一些总结
  5. FPGA学习:TMDS协议编码与解码测试
  6. VEP 注释工具 Ubuntu 安装
  7. 微信浏览器 html5 语音,html5录音支持pc和Android、ios部分浏览器,微信也是支持的,JavaScript getUserMedia...
  8. BZOJ1500维修数列 [待修缮]
  9. 医疗行业如何应对网络安全问题?有什么合适的解决方案?
  10. 23927-13-1,Cyclo(-D-Ala-D-Ala),(3R,6R)-3,6-Dimethyl-2,5-piperazinedione