博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Javascript 风格向导
阅读量:7088 次
发布时间:2019-06-28

本文共 3285 字,大约阅读时间需要 10 分钟。

 
  大部分针对Javascript最合理的方法归纳。
 
类型
 
• 原始类型:我们可以直接使用值。
  ο  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

  

Objects
 
• 使用{}创建对象。
// badvar item = new Object();// goodvar item = {};

 

• 不要使用保留字作为关键字。

// badvar superman = {  class: 'superhero',  default: { clark: 'kent' },  private: true};// goodvar superman = {  klass: 'superhero',  defaults: { clark: 'kent' },  hidden: true};

 

Arrays
 
 • 使用[]创建数组
// badvar items = new Array();// goodvar items = [];

 • 如果你不知道数组长度,使用Array#push。

var someStack = [];// badsomeStack[someStack.length] = 'abracadabra';// goodsomeStack.push('abracadabra');

 

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

var len = items.length,    itemsCopy = [],    i;// badfor (i = 0; i < len; i++) {  itemsCopy[i] = items[i];}// gooditemsCopy = items.slice();

 

Strings

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

// badvar name = "Bob Parr";// goodvar name = 'Bob Parr';// badvar fullName = "Bob " + this.lastName;// goodvar fullName = 'Bob ' + this.lastName;

 • 超过80个字符的字符串,我们使用串联符号(\),让字符串多行显示。

 • 注意:如果过度使用带串联符号的字符可能会影响到性能。

 

// badvar 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.';// badvar 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.';// goodvar 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;// badfunction inbox(messages) {  items = '
    '; for (i = 0; i < length; i++) { items += '
  • ' + messages[i].message + '
  • '; } return items + '
';}// goodfunction inbox(messages) { items = []; for (i = 0; i < length; i++) { items[i] = messages[i].message; } return '
  • ' + items.join('
  • ') + '
';}

 

Functions

  • 函数表达式

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

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

// badif (currentUser) {  function test() {    console.log('Nope.');  }}// goodif (currentUser) {  var test = function test() {    console.log('Yup.');  };}

 

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

// badfunction nope(name, options, arguments) {  // ...stuff...}// goodfunction yup(name, options, args) {  // ...stuff...}

 

总结
 
  这些很多是大家都比较清楚的,平时经常用,我只是强调一下,让大家再复习一下。
 
  下一篇:

 

推荐
 

 

 
  

转载地址:http://oxyql.baihongyu.com/

你可能感兴趣的文章
72.11. this is incompatible with sql_mode=only_full_group_by
查看>>
C# 海康DVR客户端开发系列(3)—— 连接DVR和图像预览
查看>>
为创业我做了十年的程序员,你告诉我“程序员不适合创业”?!
查看>>
mokoid android open source HAL hacking in a picture
查看>>
RCF库ClientStub.setAutoReconnect
查看>>
Google Chrome Resize Plugin
查看>>
java编程之:Unsafe类
查看>>
序列作为主键使用的原理、优缺点讨论
查看>>
iOS - AutoLayout
查看>>
如何将dubbo封装成http协议
查看>>
Android版本和API Level对应关系
查看>>
[20150806]scn headroom.txt
查看>>
使用shell脚本查看数据库负载情况
查看>>
【MOS】12c DataPump EXPORT (EXPDP) Enhancements (文档 ID 2171666.1)
查看>>
2018年,5个关于区块链趋势的基本预测
查看>>
美国洛杉矶之行
查看>>
教你用深度学习LSTM网络预测流行音乐趋势(附代码)
查看>>
JSP 内置对象Response
查看>>
nginx配置flv服务器
查看>>
开源远程控制RealVNC源代码中的通讯协议RFB(远程帧缓冲)(转)
查看>>