首页 > 代码库 > Javascript 正确使用方法

Javascript 正确使用方法

今天在github 上面找到了一个关于如何正确使用javascript 来进行我们的程序开发.我就恬不知耻的来了个原创啊..坑爹啊.拿来和大家分享一下吧.

A mostly reasonable approach to Javascript.

    Types //类型
    Objects //对象
    Arrays //数组
    Strings //字符串
    Functions //函数
    Properties //属性
    Variables //变量
    Hoisting //变量提升
    Conditional Expressions & Equality //条件表达式和等式.
    Blocks //块代码
    Comments //注释
    Whitespace //空格
    Commas  //逗号
    Semicolons //分号
    Type Casting & Coercion //类型转换
    Naming Conventions //命名规则
    Accessors //访问
    Constructors //构造器
    Events //时间
    Modules //模型
    jQuery //
    ECMAScript 5 Compatibility //ECMA 5 兼容
    Testing //测试
    Performance //性能
    Resources //资源
    In the Wild
    Translation
    The JavaScript Style Guide Guide
    Contributors
    License


Types (类型)

        原始类型: 当访问一个原始类型的时候,其实直接访问该原始类型的内容.

    string

   number

   boolean

   null

   undefined

 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


object(对象)

使用对象字面量来创建对象 (literal)

//bad

var item = new Object();

//good

var item = {};

不要使用保留关键字作为对象的属性名.这在IE8下无法工作.

//bad

var superman = {

  default: {clark: ‘kent‘},

  private: true

};

//good

var superman = {

  defaults: {clark: ‘kent‘},

  hidden: true

};

array(数组)

同样使用 字面量方法来创建数组

//bad

var items = new Array();

//good

var items = [];

如果你不知道数组的长度,那么使用Array的内置方法push进行插入操作

var someStack = [];

//bad

someStack[someStack.length] = ‘vein‘;

//good

someStack.push(‘vein‘);


当你想要拷贝一个数组的时候,使用array.slice

var len = items.length, //指的就是上面的内容...

  itemCopy = [],

  i;

//bad

for(i = 0; i < len ; ++i){

    itemCopy[i] = items[i];

}

//good

itemCopy = items.slice(); //这里要注意了.这个我还真不知道...


Strings 字符串

使用单引号 (single quotes ) 来包围字符串...//这里我没有找到合适的关于性能方面的解释,我个人也喜欢这么用,(穿的少总比穿得多好看点吧..你懂得..)

//bad

var name = "Bob Parr";

//good

var name = ‘Bob Parr‘;

//bad

var fullName = "Bob " + this.lastName;

//good

var fullName = ‘Bob ‘ + this.lastName;

字符串长于80个字符的时候需要使用字符串连接在多行进行编写..注意,如果过度使用,连接字符串将会影响性能(performance)

    // 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 效果会更好..

var items,

    messages,

    length,

    i;

messages = [{

   stat: ‘success‘,

   message: ‘ This one worked‘

},{

  stat: ‘success‘,

   message: ‘ This one worked‘

},{

  stat: ‘success‘,

   message: ‘ This one worked‘

}

];

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>‘;

}


函数(Functions)

//匿名函数表达式..

var anonymous = function(){

   return true;

};

// 命名函数表达式.

var named = function named(){

  return true;

};

//即时引用函数

(function(){

  console.log(‘Welcome to the Internet. Please follow me.‘);

})();

永远不要在非函数的块代码(if,while)中定义函数.相应的,在代码块中间函数赋值给外部的变量名..

//bad

if(currentUser){

  function test(){

    console.log(‘Nope.‘);

  }

}

//good

var test;

if(currentUser){

  test = function(){

    console.log(‘Yup‘); 

  };  //be careful with the semi-colon.

}

Properties (属性)

使用点语法来访问属性.

var luke = {

  jedi: true,

  age: 28

};

//bad

var isJedi = luke[‘jedi‘];

//good

var isJedi = luck.jedi;

当使用变量访问对象属性时,使用 [] 方括号来访问

    var luke = {
      jedi: true,
      age: 28
    };

    function getProp(prop) {
      return luke[prop];
    }

    var isJedi = getProp(‘jedi‘);


下一篇接着来....

Best wishes..