找回密码
 立即注册
首页 业界区 业界 Javascript 风格向导

Javascript 风格向导

咒卖箴 2025-5-29 16:36:23
序   大部分针对Javascript最合理的方法归纳。 类型 • 原始类型:我们可以直接使用值。  ο  string  ο  number  ο  boolean  ο  null  ο  undefined
  1. var foo = 1,
  2.     bar = foo;
  3. bar = 9;
  4. console.log(foo, bar); // => 1, 9
复制代码
•   复合类型:我们通过`引用`对值进行间接访问。
  ο  object

  ο  array
  ο  function
 
  1. var foo = [1, 2],
  2.     bar = foo;
  3. bar[0] = 9;
  4. console.log(foo[0], bar[0]); // => 9, 9
复制代码
  
Objects • 使用{}创建对象。
  1. // bad
  2. var item = new Object();
  3. // good
  4. var item = {};
复制代码
 
• 不要使用保留字作为关键字。
  1. // bad
  2. var superman = {
  3.   class: 'superhero',
  4.   default: { clark: 'kent' },
  5.   private: true
  6. };
  7. // good
  8. var superman = {
  9.   klass: 'superhero',
  10.   defaults: { clark: 'kent' },
  11.   hidden: true
  12. };
复制代码
 
Arrays  • 使用[]创建数组
  1. // bad
  2. var items = new Array();
  3. // good
  4. var items = [];
复制代码
 • 如果你不知道数组长度,使用Array#push。
  1. var someStack = [];
  2. // bad
  3. someStack[someStack.length] = 'abracadabra';
  4. // good
  5. someStack.push('abracadabra');
复制代码
 
  • 当你需要复制数组的时候,请使用Array#slice。
  1. var len = items.length,
  2.     itemsCopy = [],
  3.     i;
  4. // bad
  5. for (i = 0; i < len; i++) {
  6.   itemsCopy[i] = items[i];
  7. }
  8. // good
  9. itemsCopy = items.slice();
复制代码
 
Strings • 对于字符串,我们使用单引号''。
  1. // bad
  2. var name = "Bob Parr";
  3. // good
  4. var name = 'Bob Parr';
  5. // bad
  6. var fullName = "Bob " + this.lastName;
  7. // good
  8. var fullName = 'Bob ' + this.lastName;
复制代码
 • 超过80个字符的字符串,我们使用串联符号(\),让字符串多行显示。
 • 注意:如果过度使用带串联符号的字符可能会影响到性能。
1.png
 
  1. // bad
  2. 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.';
  3. // bad
  4. var errorMessage = 'This is a super long error that \
  5. was thrown because of Batman. \
  6. When you stop to think about \
  7. how Batman had anything to do \
  8. with this, you would get nowhere \
  9. fast.';
  10. // good
  11. var errorMessage = 'This is a super long error that ' +
  12.   'was thrown because of Batman.' +
  13.   'When you stop to think about ' +
  14.   'how Batman had anything to do ' +
  15.   'with this, you would get nowhere ' +
  16.   'fast.';
复制代码
 
  
 • 当我们在编程的时候,需要拼接出一个字符串,我们可以使用Array#join 代替字符串连接。尤其是对IE浏览器。 
  1. var items,
  2.     messages,
  3.     length, i;
  4. messages = [{
  5.     state: 'success',
  6.     message: 'This one worked.'
  7. },{
  8.     state: 'success',
  9.     message: 'This one worked as well.'
  10. },{
  11.     state: 'error',
  12.     message: 'This one did not work.'
  13. }];
  14. length = messages.length;
  15. // bad
  16. function inbox(messages) {
  17.   items = '<ul>';
  18.   for (i = 0; i < length; i++) {
  19.     items += '<li>' + messages[i].message + '</li>';
  20.   }
  21.   return items + '</ul>';
  22. }
  23. // good
  24. function inbox(messages) {
  25.   items = [];
  26.   for (i = 0; i < length; i++) {
  27.     items[i] = messages[i].message;
  28.   }
  29.   return '<ul><li>' + items.join('</li><li>') + '</li></ul>';
  30. }
复制代码
 
Functions  • 函数表达式
  1. // anonymous function expression
  2. var anonymous = function() {
  3.   return true;
  4. };
  5. // named function expression
  6. var named = function named() {
  7.   return true;
  8. };
  9. // immediately-invoked function expression (IIFE)
  10. (function() {
  11.   console.log('Welcome to the Internet. Please follow me.');
  12. })();
复制代码
 • 绝对不要在非函数块(if,while)申明一个函数。我们可以把函数申明变成一个函数表达式。
  1. // bad
  2. if (currentUser) {
  3.   function test() {
  4.     console.log('Nope.');
  5.   }
  6. }
  7. // good
  8. if (currentUser) {
  9.   var test = function test() {
  10.     console.log('Yup.');
  11.   };
  12. }
复制代码
 
 • 绝对不要把一个参数命名为arguments,arguments参数是函数作用域内给出的一个特殊变量,如果你把参数命名为arguments,那么这个参数就会覆盖它原有的特殊变量。
  1. // bad
  2. function nope(name, options, arguments) {
  3.   // ...stuff...
  4. }
  5. // good
  6. function yup(name, options, args) {
  7.   // ...stuff...
  8. }
复制代码
 
总结   这些很多是大家都比较清楚的,平时经常用,我只是强调一下,让大家再复习一下。   下一篇:Javascript 风格向导(续) 
推荐 
2.png
 
   
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册