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 ...
随机推荐
- J2EE环境搭建(三)配置Tomcat 7.0的局部数据源
在J2EE环境搭建(一)中遗留下一个配置Tomcat数据源的问题,最近都在专心搞iOS的东西,由于J2EE布置了作业,所以又回过头来搞下J2EE.汗... 在这里我使用的是MySQL. 1.配置MyS ...
- ZendServer中关于php.ini不同环境的建议
ZendServer根据开发环境和产品环境的不同情况,对php.ini中的一些选项做了建议设置,列表如下: ;;;;;;;;;;;;;;;;;;; ; Quick Reference ; ;;;;;; ...
- iOS触摸事件哦
主要是记录下iOS的界面触摸事件处理机制,然后用一个实例来说明下应用场景. 一.处理机制 界面响应消息机制分两块,(1)首先在视图的层次结构里找到能响应消息的那个视图.(2)然后在找到的视图里处理消息 ...
- Request to https://bower.herokuapp.com failed with 502
bower 版本过低,需要升级为最新bower版本, 如果升级版本后依然无法使用,更改.bowerrc配置,如下所示 { "directory": "bower_comp ...
- hadoop2.7.0实践- WordCount
环境要求 说明:本文档为wordcount的mapreduce job编写及执行文档. 操作系统:Ubuntu14 x64位 Hadoop:Hadoop 2.7.0 Hadoop官网:http://h ...
- Heap 3214 LIS题解
依据问题转换成最长不降子序列问题. 10^9的输入数据计算起来还是挺花时间的.由于这里仅仅能使用O(nlgn)时间复杂度了. 只是证明是能够算出10^9个数据的. 由于时间限制是5s. #includ ...
- C# DateTime 时间格式
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- Hive查询表,返回数据全是NULL
情况1: hive> create table users(id int, name string); hive> load data local inpath '/usr/local/u ...
- 常见typedef 用法
1.常规变量类型定义例如:typedef unsigned char uchar描述:uchar等价于unsigned char类型定义 uchar c声明等于unsigned char c ...
- h5-文本框
h5-文本框 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...