注册 | 登陆
您的位置:阿里西西 > 前端技术 > 脚本技术 > 详细内容

javascript: Javascript 风格向导

  稿源:互联网   2013-07-11 16:54:48   点击:   撤稿纠错

以下讨论的是和相关的javascript: Javascript 风格向导 教程文章,内容是本站精心挑选整理的教程,希望对广大的网友给到帮助,下面是详细内容:



  大部分针对Javascript最合理的方法归纳。
类型
? 原始类型:我们可以直接使用值。
  ο  string
  ο  number
  ο  boolean
  ο  null
  ο  undefined
SearchIndex
var foo = 1,
bar = foo;
bar = 9;
console.log(foo, bar); // => 1, 9
SearchIndex
?   复合类型:我们通过`引用`对值进行间接访问。
  ο  object
  ο  array
  ο  function
SearchIndex
var foo = [1, 2],
bar = foo;
bar[0] = 9;
console.log(foo[0], bar[0]); // => 9, 9
SearchIndex
 
Objects
? 使用{}创建对象。
// bad
var item = new Object();
// good
var item = {};
? 不要使用保留字作为关键字。
SearchIndex
// bad
var superman = {
class: 'superhero',
default: { clark: 'kent' },
private: true
};
// good
var superman = {
klass: 'superhero',
defaults: { clark: 'kent' },
hidden: true
};
SearchIndex
Arrays
? 使用[]创建数组
// bad
var items = new Array();
// good
var items = [];
? 如果你不知道数组长度,使用Array#push。
SearchIndex
var someStack = [];
// bad
someStack[someStack.length] = 'abracadabra';
// good
someStack.push('abracadabra');
SearchIndex
  ? 当你需要复制数组的时候,请使用Array#slice。
SearchIndex
var len = items.length,
itemsCopy = [],
i;
// bad
for (i = 0; i < len; i++) {
itemsCopy[i] = items[i];
}
// good
itemsCopy = items.slice();
SearchIndex
Strings
? 对于字符串,我们使用单引号''。
SearchIndex
// bad
var name = "Bob Parr";
// good
var name = 'Bob Parr';
// bad
var fullName = "Bob " + this.lastName;
// good
var fullName = 'Bob ' + this.lastName;
SearchIndex
? 超过80个字符的字符串,我们使用串联符号(),让字符串多行显示。
? 注意:如果过度使用带串联符号的字符可能会影响到性能。
 
SearchIndex
// 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.';
SearchIndex
  
? 当我们在编程的时候,需要拼接出一个字符串,我们可以使用Array#join 代替字符串连接。尤其是对IE浏览器。
SearchIndex
var items,
messages,
length, i;
messages = [{
state: 'success', 本文链接http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130709/39005.html


关于javascript: Javascript 风格向导的内容写到这里就结束啦,您可以收藏本页网址http://www.alixixi.com/web/ a/2013071189745.shtml方便下次再访问哦。