JavaScript Best Practices
原文: 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
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:
var i;
// Use later
for (i = 0; i < 5; i++) {
JavaScript Best Practices的更多相关文章
- JavaScript Best Practices (w3cschool)
JavaScript Best Practices (w3cschool) Local Variables: · 总是在前面集中定义变量,(包括 for 的i).(strict mode) ...
- JavaScript best practices JS最佳实践
JavaScript best practices JS最佳实践 0 简介 最佳实践起初比较棘手,但最终会让你发现这是非常明智之举. 1.合理命名方法及变量名,简洁且可读 var someItem = ...
- 24 javascript best practices for beginner(only 23 finally)
原文是英文,链接: http://net.tutsplus.com/tutorials/JavaScript-ajax/24-JavaScript-best-practices-for-beginne ...
- Javascript函数重载,存在呢—还是存在呢?
1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...
- 10 个你需要了解的最佳 javascript 开发实践
原文:Top 10 “Must Follow” JavaScript Best Practices Javascript 的很多扩展的特性是的它变得更加的犀利, 同时也给予程序员机会创建更漂亮并且更让 ...
- 给JavaScript初学者的24条最佳实践(转:http://www.cnblogs.com/yanhaijing/p/3465237.html)
作为“30 HTML和CSS最佳实践”的后续,本周,我们将回顾JavaScript的知识 !如果你看完了下面的内容,请务必让我们知道你掌握的小技巧! 1.使用 === 代替 == JavaScript ...
- JavaScript初学者应知的24条最佳实践(译)
原文:24 JavaScript Best Practices for Beginners 译者:youngsterxyf (注:阅读原文的时候没有注意发布日期,觉得不错就翻译了,翻译到JSON.pa ...
- (译) 《Javascript 24条最佳实践》
(摘录) <Javascript 24条最佳实践> 自己一直偏向于实用主义,不是学院派,不是学究派,只讲究把东西能够很好的做出来,但经过一段时间的开发工作当自己总结出来一些东西时,觉得挺有 ...
- javascript 私有方法的实现
原文地址: http://frugalcoder.us/post/2010/02/11/js-classes.aspx Classy JavaScript - Best Practices 11. F ...
随机推荐
- winform中key读取修改
根据key name的名称读取value-----读取使用ConfigurationManager.AppSettings读取容易没读取到根目录中的key public string GetXml(s ...
- IE6对!important单个的类是支持的
"!important"是什么? 第一个,是设置样式的优先级,设了!important的样式的属性优先于id选择器和class选择器.,比如id为"Main"的 ...
- bestcoder15_love
#include <iostream> #include <stdio.h> #include <string.h> #include <vector> ...
- 使用keepalived监控tomcat 达到双机热备
通常说的双机热备是指两台机器都在运行,但并不是两台机器都同时在提供服务. 当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,而且切换的时间非常短.下面来以keepalived结合to ...
- nutch 2.1安装问题集锦
参照官方文档http://nlp.solutions.asia/?p=180 中间碰到的问题,解决方法参考 http://blog.javachen.com/2014/05/20/nutch-intr ...
- gvim设置使用
最近有一款编辑器叫sublimeText 2比较流行,我也下载用了一下,确实很好看,自动完成,缩进功能什么的也比较齐全,插件也十分丰富.但用起来不是很顺手,最后还是回到了Gvim(Vim的GUI版本, ...
- IaaS, PaaS和SaaS
原文链接:http://www.leiphone.com/news/201406/iaas-paas-and-saas.html 云服务”现在已经快成了一个家喻户晓的词了.如果你不知道PaaS, Ia ...
- 64位操作系统安装32位客户端和PL/SQL
PL/SQ只能使用32位的Oracle客户端.在64位系统下安装了64位的oracle 11g,使用PL/SQL需再安装32位Oracle客户端. 按以下方法试验成功: 1)安装32位的Oracle客 ...
- VS2013 Sqlite3 操作指令
extern "C"{ #include "sqlite3.h" }; #pragma comment(lib,"sqlite.lib") ...
- android 研究的环境搭建、高效工具、网站资源
================= 2015 年 10 月 14 号 更新 著名的android开源社区xda有一个帖子,详细描述了android开发和研究环境的初始搭建过程: http://for ...