原文: 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 Best Practices (w3cschool)

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

  2. JavaScript best practices JS最佳实践

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

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

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

  4. Javascript函数重载,存在呢—还是存在呢?

    1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...

  5. 10 个你需要了解的最佳 javascript 开发实践

    原文:Top 10 “Must Follow” JavaScript Best Practices Javascript 的很多扩展的特性是的它变得更加的犀利, 同时也给予程序员机会创建更漂亮并且更让 ...

  6. 给JavaScript初学者的24条最佳实践(转:http://www.cnblogs.com/yanhaijing/p/3465237.html)

    作为“30 HTML和CSS最佳实践”的后续,本周,我们将回顾JavaScript的知识 !如果你看完了下面的内容,请务必让我们知道你掌握的小技巧! 1.使用 === 代替 == JavaScript ...

  7. JavaScript初学者应知的24条最佳实践(译)

    原文:24 JavaScript Best Practices for Beginners 译者:youngsterxyf (注:阅读原文的时候没有注意发布日期,觉得不错就翻译了,翻译到JSON.pa ...

  8. (译) 《Javascript 24条最佳实践》

    (摘录) <Javascript 24条最佳实践> 自己一直偏向于实用主义,不是学院派,不是学究派,只讲究把东西能够很好的做出来,但经过一段时间的开发工作当自己总结出来一些东西时,觉得挺有 ...

  9. javascript 私有方法的实现

    原文地址: http://frugalcoder.us/post/2010/02/11/js-classes.aspx Classy JavaScript - Best Practices 11. F ...

随机推荐

  1. 设置(settings)

    设置(settings) 题目描述 如题所示,这将是一个关于设置的问题. 你需要通过对一个控制台进行设置,来得到不同的效果. 这个控制台由n个控制元件组成,每个元件有m种设置,其中i号元件的第j种设置 ...

  2. JavaScript各变量类型的判断方法

    我们很容易被漂亮的代码吸引,也不知不觉的在自己的代码库中加入这些.却没有冷静的想过它们的优劣.这不,我就收集了一系列形如 "是否为……?" 的判断的boolean函数. isNul ...

  3. bzoj1185 [HNOI2007]最小矩形覆盖 旋转卡壳求凸包

    [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 2081  Solved: 920 ...

  4. MFC 加载资源文件里的png

    static bool LoadImageFromResource(IN CImage* pImage, IN UINT nResID, IN LPCWSTR lpTyp) { if ( pImage ...

  5. js对象和jq对象互相转换

    1.DOM 对象转成 jQuery 对象 var v = document.getElementById("v"); //DOM对象 var $v = $(v); //jQuery ...

  6. RSA加密/解密 Decryption error异常解决

    RSA加密/解密 Decryption error异常解决 import java.io.ByteArrayOutputStream; import java.security.Key; import ...

  7. 昨天用到的一个sql查询。可取处,用max

    SELECT T_AssetInfos_ID, MAX(T_AssetConstruct_Name), MAX(T_AssetProperties_Name), SUM(CAST(PropertyVa ...

  8. C#操作windows事件日志项

    /// <summary> /// 指定事件日志项的事件类型 /// </summary> public enum EventLogLevel { /// <summar ...

  9. centos6.5 网卡服务开机自动启动

    今天打开许久没用的centos之后,发现网络服务器不可用,通过service network restart 依然无法使用,简单记录一下处理过程: 1.通过setup 命令查看Network conf ...

  10. centos7 svn服务器搭建

    系统环境:centos 7.5 yum安装svn yum install subversion -y 创建svn 版本库目录 mkdir -p /var/svn/svnrepos 在版本库目录下创建版 ...