js 风格(注意事项)

- var foo = 1,
- bar = foo;
- bar = 9;
- console.log(foo, bar); // => 1, 9

• 复合类型:我们通过`引用`对值进行间接访问。
ο object
ο array
ο function

- var foo = [1, 2],
- bar = foo;
- bar[0] = 9;
- console.log(foo[0], bar[0]); // => 9, 9

- // bad
- var item = new Object();
- // good
- var item = {};
• 不要使用保留字作为关键字。

- // bad
- var superman = {
- class: 'superhero',
- default: { clark: 'kent' },
- private: true
- };
- // good
- var superman = {
- klass: 'superhero',
- defaults: { clark: 'kent' },
- hidden: true
- };

• 使用[]创建数组
- // bad
- var items = new Array();
- // good
- var items = [];
• 如果你不知道数组长度,使用Array#push。

- var someStack = [];
- // bad
- someStack[someStack.length] = 'abracadabra';
- // good
- someStack.push('abracadabra');

• 当你需要复制数组的时候,请使用Array#slice。

- var len = items.length,
- itemsCopy = [],
- i;
- // bad
- for (i = 0; i < len; i++) {
- itemsCopy[i] = items[i];
- }
- // good
- itemsCopy = items.slice();

• 对于字符串,我们使用单引号''。

- // bad
- var name = "Bob Parr";
- // good
- var name = 'Bob Parr';
- // bad
- var fullName = "Bob " + this.lastName;
- // good
- var fullName = 'Bob ' + this.lastName;

• 超过80个字符的字符串,我们使用串联符号(\),让字符串多行显示。
• 注意:如果过度使用带串联符号的字符可能会影响到性能。

- // bad
- var errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
- // bad
- var errorMessage = 'This is a super long error that \
- was thrown because of Batman. \
- When you stop to think about \
- how Batman had anything to do \
- with this, you would get nowhere \
- fast.';
- // good
- var errorMessage = 'This is a super long error that ' +
- 'was thrown because of Batman.' +
- 'When you stop to think about ' +
- 'how Batman had anything to do ' +
- 'with this, you would get nowhere ' +
- 'fast.';

• 当我们在编程的时候,需要拼接出一个字符串,我们可以使用Array#join 代替字符串连接。尤其是对IE浏览器。

- var items,
- messages,
- length, i;
- messages = [{
- state: 'success',
- message: 'This one worked.'
- },{
- state: 'success',
- message: 'This one worked as well.'
- },{
- state: 'error',
- message: 'This one did not work.'
- }];
- length = messages.length;
- // bad
- function inbox(messages) {
- items = '<ul>';
- for (i = 0; i < length; i++) {
- items += '<li>' + messages[i].message + '</li>';
- }
- return items + '</ul>';
- }
- // good
- function inbox(messages) {
- items = [];
- for (i = 0; i < length; i++) {
- items[i] = messages[i].message;
- }
- return '<ul><li>' + items.join('</li><li>') + '</li></ul>';
- }

• 函数表达式

- // anonymous function expression
- var anonymous = function() {
- return true;
- };
- // named function expression
- var named = function named() {
- return true;
- };
- // immediately-invoked function expression (IIFE)
- (function() {
- console.log('Welcome to the Internet. Please follow me.');
- })();

• 绝对不要在非函数块(if,while)申明一个函数。我们可以把函数申明变成一个函数表达式。

- // bad
- if (currentUser) {
- function test() {
- console.log('Nope.');
- }
- }
- // good
- if (currentUser) {
- var test = function test() {
- console.log('Yup.');
- };
- }

• 绝对不要把一个参数命名为arguments,arguments参数是函数作用域内给出的一个特殊变量,如果你把参数命名为arguments,那么这个参数就会覆盖它原有的特殊变量。

- // bad
- function nope(name, options, arguments) {
- // ...stuff...
- }
- // good
- function yup(name, options, args) {
- // ...stuff...
- }
js 风格(注意事项)的更多相关文章
- 「标准」的 JS风格
首先,这份 JS风格指南已经在我司的前端团队实行半年多了: 其次,在程序员的世界里,从入行到资深都需要面对几个世界级的难题,如: 世界上最好的编辑器是什么? 是用空格还是 TAB?用空格还特么衍生出 ...
- Vue.js 使用注意事项
Vue.js 使用注意事项 1 过滤器主要用于简单的文本转换,如果要实现复杂的数据变换,应使用计算属性 指令的使用 v-bind基本用于HTML元素上的属性,如id.class.href.src等 v ...
- soui使用wke时,设置js回调注意事项
wke响应网页js函数调用时注意: 必须等网页加载完成后,才能通过SetJsFunc设置js函数与c++回调的对应.网页未加载就设置,不会响应c++函数. 示例代码: wkeJSData* data ...
- js兼容注意事项--仅供参考
做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...
- JS中注意事项
(一)判断中注意事项 一.所有的相对路径都别拿来做判断 1.img src='...' 2.href='1.css', href='html/index.html' 3.img src='http:/ ...
- 关于HTML中标签<a>使用js的注意事项
以下两点都不可取: 1.<a href="#" onClick="popUp('http://www.baidu.com');return false;" ...
- 最近兰州的js风格写个插件和一个template engine
/* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...
- js风格技巧
1.一个页面的所有js都可以写成这样,比如: var index ={}; index.User = ****; index.Init = function(){ $("$tes ...
- js操作注意事项
1.函数赋值给变量时,不能加括号 function fun() { ... } var str=fun; 2.js创建构造函数和调用对象,对象内不能用var 变量,只能用this function f ...
随机推荐
- Adb分析及获取root权限
Adb的全称为Android Debug Bridge,起到通过PC对Android系统的调试桥的作用,是一个多用途的工具,它能够执行多种命令,还能提供一个shell.这儿简单介绍一下Adb的代码结构 ...
- poj 1274 The Perfact Stall
click here ~~ ***The Perfect Stall*** Description Farmer John completed his new barn just last week, ...
- C语言之基本算法08—去掉最高分去掉最低分求平均值
// /* ================================================================== 题目:选拔赛中通经常使用这种办法求选手分数,去掉一个最 ...
- css hacks
/***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...
- 关于wcf,webservice,webapi或者其他服务或者接口有什么区别 WCF、WebAPI、WebService之间的区别 【转载】HTTP和SOAP完全就是两个不同的协议 WebService学习总结(一)——WebService的相关概念
wcf,webservice采用的是rpc协议,这个协议很复杂,所以每次要传递.要校验的内容也很复杂,别看我们用的很简单,但实际是frame帮我们做掉了rpc生成.解析的事情webapi遵循是rest ...
- VS2012安装git
新安装了vs2012,尝试使用Git,安装过程比较简单,记录下来以备以后查阅. 1. 下载.安装Git 我的系统是Windows 7,需要安装Git for Windows. 下载地址: http ...
- Chisel Tutorial(六)——port
下面内容根据2015-7-10版的Chisel 2.2 Tutorial整理 port就是硬件单元对外的接口,须要指明方向(输入还是输出).一个port声明的样例例如以下: class Decoupl ...
- Zip 压缩问题件,获取真实扩展名
].InputStream); foreach (var element in _ZIP.Entries) { using (System.IO.StreamReader _ddd = new Sys ...
- MySQL 记录中包含换行符
换行符: \r\n \r CHAR(10) 处理方法: REPLACE(str,from_str,to_str); 如:REPLACE('aaa\nbbb','\n','');
- Jquery学习笔记(9)--注册验证复习(未用到ajax)
纯复习,在$(this).val()这里浪费了时间,val()只适合input里面的value值,如果是span等标签里包裹的文本要用text()!! <!DOCTYPE html> &l ...