File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -951,3 +951,51 @@ JavaScript中是没有常量的,尽管在一些比较现代的环境中可能
951
951
952
952
// is the value still intact?
953
953
constant.get("maxwidth"); // 480
954
+
955
+ ## 链式调用模式
956
+
957
+ 使用链式调用模式可以让你在一对个象上连续调用多个方法,不需要将前一个方法的返回值赋给变量,也不需要将多个方法调用分散在多行:
958
+
959
+ myobj.method1("hello").method2().method3("world").method4();
960
+
961
+ 当你创建了一个没有有意义的返回值的方法时,你可以让它返回this,也就是这些方法所属的对象。这使得对象的使用者可以将下一个方法的调用和前一次调用链起来:
962
+
963
+ var obj = {
964
+ value: 1,
965
+ increment: function () {
966
+ this.value += 1;
967
+ return this;
968
+ },
969
+ add: function (v) {
970
+ this.value += v;
971
+ return this;
972
+ },
973
+ shout: function () {
974
+ alert(this.value);
975
+ }
976
+ };
977
+
978
+ // chain method calls
979
+ obj.increment().add(3).shout(); // 5
980
+
981
+ // as opposed to calling them one by one
982
+ obj.increment();
983
+ obj.add(3);
984
+ obj.shout(); // 5
985
+
986
+ ### 链式调用模式的利弊
987
+
988
+ 使用链式调用模式的一个好处就是可以节省代码量,使得代码更加简洁和易读,读起来就像在读句子一样。
989
+
990
+ 另外一个好处就是帮助你思考如何拆分你的函数,创建更小、更有针对性的函数,而不是一个什么都做的函数。长时间来看,这会提升代码的可维护性。
991
+
992
+ 一个弊端是调用这样写的代码会更困难。你可能知道一个错误出现在某一行,但这一行要做很多的事情。当链式调用的方法中的某一个出现问题而又没报错时,你无法知晓到底是哪一个出问题了。《代码整洁之道》的作者Robert Martion甚至叫这种模式为“train wreck”模式。(译注:直译为“火车事故”,指负面影响比较大。)
993
+
994
+ 不管怎样,认识这种模式总是好的,当你写的方法没有明显的有意义的返回值时,你就可以返回` this ` 。这个模式应用得很广泛,比如jQuery库。如果你去看DOM的API的话,你会发现它也会以这样的形式倾向于链式调用:
995
+
996
+ document.getElementsByTagName('head')[0].appendChild(newnode);
997
+
998
+
999
+
1000
+
1001
+
You can’t perform that action at this time.
0 commit comments