File tree Expand file tree Collapse file tree 1 file changed +123
-1
lines changed Expand file tree Collapse file tree 1 file changed +123
-1
lines changed Original file line number Diff line number Diff line change 46
46
47
47
- [ 花括号的对齐方式] ( #花括号的对齐方式 )
48
48
- [ 块语句的间隔] ( #块语句的间隔 )
49
+ - [ switch语句] ( #switch语句 )
50
+ + [ 缩进] ( #缩进 )
51
+ + [ case语句的“连续执行”] ( #case语句的“连续执行” )
52
+ + [ default] ( #default )
49
53
50
54
### 缩进层级
51
55
@@ -230,7 +234,7 @@ if (wl && wl.length) {
230
234
231
235
### 命名
232
236
233
- - JavaScript语言核心是EXMAScript ,遵照了驼峰式大小写命名法(这个太有名了我就不解释了)
237
+ - JavaScript语言核心是ECMAScript ,遵照了驼峰式大小写命名法(这个太有名了我就不解释了)
234
238
- 一般是遵循语言核心所采用的命名规范,因此大部分JavaScript程序员使用驼峰命名法给变量和函数命名。
235
239
236
240
@@ -869,6 +873,124 @@ Y.merge = function() {
869
873
870
874
[返回顶部](#编程风格)
871
875
876
+ ### switch语句
877
+
878
+ - JavaScript中的` switch `语句的行为和在其他语言中是不一样的:switch语句中可以使用任意类型值,任何表达式都可合法地用于case从句。但在其他语言中则必须使用原始值和常量。
879
+
880
+ [返回顶部](#编程风格)
881
+
882
+ #### 缩进
883
+
884
+ - 很多人用java的风格
885
+ ```javascript
886
+ switch(condition) {
887
+ case " first" :
888
+ // 代码
889
+ break ;
890
+
891
+ case " second" :
892
+ // 代码
893
+ break ;
894
+
895
+ case " third" :
896
+ // 代码
897
+ break ;
898
+
899
+ default :
900
+ // 代码
901
+ }
902
+ ` ` `
903
+
904
+ - 独特之处
905
+ + 每条` case` 语句相对于` switch `关键字都缩进一个层级。
906
+ + 从第二条豫剧开始,每条`case`语句前后各有一个空行。
907
+
908
+ - 另一种风格
909
+ ```javascript
910
+ switch(condition) {
911
+ case " first" :
912
+ // 代码
913
+ break ;
914
+ case " second" :
915
+ // 代码
916
+ break ;
917
+ case " third" :
918
+ // 代码
919
+ break ;
920
+ default :
921
+ // 代码
922
+ }
923
+ ` ` `
924
+ - 不同之处是` case` 关键字与` switch `保持左对齐,
925
+
926
+ - 我和作者都喜欢java的风格。
927
+
928
+ [返回顶部](#编程风格)
929
+
930
+ #### case语句的“连续执行”
931
+
932
+ - `case`后面不加`break`,就会连续执行下面的条件,这个成为很多系统bug的原罪。
933
+ - 但是还是有许多人接受这种连续执行的编程方法。但是逻辑一定要写的清晰。
934
+ ```javascript
935
+ switch(condition) {
936
+
937
+ // 明显的依次执行
938
+ case " first" :
939
+ case " second" :
940
+ // 代码
941
+ break ;
942
+
943
+ case " third" :
944
+ // 代码
945
+
946
+ /* fall through */
947
+ default :
948
+ // 代码
949
+ }
950
+ ` ` `
951
+ - 前面2行代码是个很明显的连续执行,这是合理的。
952
+ - ` third` 里面由于添加了注释,说明这是有意为之。这也是合理的。
953
+
954
+ [返回顶部](#编程风格)
955
+
956
+ #### default
957
+
958
+ - 比较有争论的议题是,是否需要` default` ,很多人不论何时都不省略` default` ,尽管它什么也不做。
959
+ ` ` ` javascript
960
+ switch (condition) {
961
+ case " first" :
962
+ // 代码
963
+ break ;
964
+
965
+ case " second" :
966
+ // 代码
967
+ break ;
968
+
969
+ default :
970
+ // default中没有逻辑
971
+ }
972
+ ` ` `
973
+
974
+ - 作者比较倾向于,没有默认行为,并且写了注释的情况下可以省略
975
+ ` ` ` javascript
976
+ switch (condition) {
977
+ case " first" :
978
+ // 代码
979
+ break ;
980
+
981
+ case " second" :
982
+ // 代码
983
+ break ;
984
+
985
+ // 没有 default
986
+ }
987
+ ` ` `
988
+ - 这样即表明了没有默认行为,又省下了字节。
989
+
990
+ - 我觉得2中方法都可以,就看你喜欢哪一种。
991
+
992
+ [返回顶部](#编程风格)
993
+
872
994
## 补足
873
995
874
996
[图片识别](https://github.com/gsdgdf/Java_OCR)
You can’t perform that action at this time.
0 commit comments