我记得学编程的时候又听过尽量避免 method chaining ,因为其中任何一个环节出错就挂了(我记得还有个专门的术语,哪个达人记得的麻烦指点下)。但我看到写 jQuery 貌似很多时候都有用到 method chaining ,的确也会让程序更加简短,这和我理解的 mehtod chaining 有区别吗?最佳实践是什么呢?
1
shibo501c Dec 3, 2015
你说的专门术语 指的是 fluent style 吗?
|
2
tylr Dec 3, 2015 应该是 Law of Demeter, quote from [wikipedia]( https://en.wikipedia.org/wiki/Law_of_Demeter): an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code a.b.Method() breaks the law where a.Method() does not. As an analogy, when one wants a dog to walk, one does not command the dog's legs to walk directly; instead one commands the dog which then commands its own legs.
|