码迷,mamicode.com
首页 > Web开发 > 详细

js格式

时间:2017-09-22 18:59:16      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:real   actual   mes   str   操作   循环   element   需要   dmi   

  1 /**
  2  * Created by admin on 2017/9/22.
  3  */
  4 
  5 // 分号后不要再有多余的空格
  6 var name = "North";
  7 var name = "North";
  8 
  9 
 10 
 11 // 条件匹配简写
 12 if (x) {
 13     if (x.name) {
 14         console.log(x.name);
 15     }
 16 }
 17 
 18 if (x && x.name) {
 19     console.log(x.name);
 20 }
 21 
 22 
 23 
 24 // 类似lambda表达式
 25 if (name) {
 26     return f1();
 27 } else {
 28     return f2();
 29 }
 30 
 31 return name ? f1() : f2();
 32 
 33 
 34 
 35 // 定义变量为标签时外层用单引号内层属性用双引号
 36 var element = "<button class=‘btn‘>Click Me</button>";
 37 
 38 var element = ‘<button class="btn">Click Me</button>‘;
 39 
 40 
 41 
 42 // 对于较短的数组对象写在一行,并且用空格隔开
 43 var array = [
 44     1,
 45     2,
 46     3
 47 ];
 48 var object = {
 49     a: 1,
 50     b: 2,
 51     c: 3
 52 };
 53 
 54 var array = [1, 2, 3];
 55 var object = {a: 1, b: 2, c: 3};
 56 
 57 
 58 
 59 // 对于较长的数组对象每行一个
 60 var array = [
 61     ‘66666666666666666666‘, ‘66666666666666666666‘, ‘66666666666666666666‘, ‘66666666666666666666‘];
 62 var object = {a: ‘66666666666666666666‘, b: ‘66666666666666666666‘, c: ‘66666666666666666666‘};
 63 
 64 var array = [
 65     ‘66666666666666666666‘,
 66     ‘66666666666666666666‘,
 67     ‘66666666666666666666‘,
 68     ‘66666666666666666666‘
 69 ];
 70 var object = {
 71     a: ‘66666666666666666666‘,
 72     b: ‘66666666666666666666‘,
 73     c: ‘66666666666666666666‘
 74 };
 75 
 76 
 77 
 78 // 定义数组对象时直接使用[]{}
 79 var myArray = new Array(1, 2, 3);
 80 var myObject = new Object();
 81 myObject.a = 0;
 82 myObject.b = 1;
 83 myObject.c = 2;
 84 
 85 var myArray = [1, 2, 3];
 86 var myObject = {a: 0, b:1, c:2};
 87 
 88 
 89 
 90 // 对于较长需要换行的字符串每次换行用+拼接
 91 var myString = ‘A rather long string of English text, an error message  92     actually that just keeps going and going -- an error  93     message that is really really long.‘;
 94 
 95 var myString = ‘A rather long string of English text, an error message‘ +
 96     ‘actually that just keeps going and going -- an error‘ +
 97     ‘message that is really really long.‘;
 98 
 99 
100 
101 // 传入的参数必须是必要的,否则就不要定义
102 function foo(fun, a, b) {
103     fun.onclick = bar();
104 }
105 
106 function foo(fun, a, b) {
107     fun.onclick = bar(a, b);
108 }
109 
110 
111 
112 // 布尔值直接定义true跟false,不要用0、[]、{}
113 var x = new Boolean(0);
114 if (x) {
115     alert(‘false‘);
116 }
117 
118 var x = new Boolean(false);
119 if (x) {
120     alert(‘false‘);
121 }
122 
123 
124 
125 // 使用forEach跟for进行循环操作,避免使用for-in
126 myArray = [‘a‘, 1, ‘etc‘];
127 for (var indexNum in myArray) {
128     console.log(myArray[indexNum]);
129 }
130 
131 myArray.forEach(function(val) {
132     console.log(val);
133 });

 

js格式

标签:real   actual   mes   str   操作   循环   element   需要   dmi   

原文地址:http://www.cnblogs.com/bfmq/p/7576222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!