This question already has an answer here:
这个问题在这里已有答案:
- How does basic object/function chaining work in javascript? 5 answers
基本对象/函数链如何在javascript中工作? 5个答案
I recently started learning AngularJS using the Google documentation, and I ran into a notation I am unfamiliar with.
我最近开始使用Google文档学习AngularJS,我遇到了一个我不熟悉的符号。
The general syntax that I am confused with is as follows:
我困惑的一般语法如下:
someObject(someParams)
.SomeFunction()
.SomeFunction()
.SomeFunction()
The problem is that typically I am used to seeing
问题是我通常习惯于看到
SomeObject.someFunction()
SomeObject.someFunction()
SomeObject.someFunction()
Can someone please clarify what is going on here? Am I missing something? If it helps at all, I teach Java Programming and am very good with C# and Java so maybe that can help someone gear up an answer for me.
有人可以澄清这里发生了什么吗?我错过了什么吗?如果它有帮助,我会教Java编程并且非常适合使用C#和Java,所以也许这可以帮助某人为我设置答案。
1 个解决方案
#1
0
This is method chaining, and it's common in many libraries, including AngularJS, and jQuery.
这是方法链,它在许多库中很常见,包括AngularJS和jQuery。
If a method is not used to return new information (such as getting the value of an input, or an element attribute), the normal behavior is for it to return the same object that it was called on. This way, instead of having to say SomeObject.
repeatedly, you can simply call the next method on the return value of the previous method. Because of this, the chained methods are equivalent to the code you're used to seeing.
如果未使用方法返回新信息(例如获取输入值或元素属性),则正常行为是返回调用它的同一对象。这样,而不必说SomeObject。反复地,您可以简单地在前一个方法的返回值上调用next方法。因此,链式方法等同于您以前看到的代码。
#1
0
This is method chaining, and it's common in many libraries, including AngularJS, and jQuery.
这是方法链,它在许多库中很常见,包括AngularJS和jQuery。
If a method is not used to return new information (such as getting the value of an input, or an element attribute), the normal behavior is for it to return the same object that it was called on. This way, instead of having to say SomeObject.
repeatedly, you can simply call the next method on the return value of the previous method. Because of this, the chained methods are equivalent to the code you're used to seeing.
如果未使用方法返回新信息(例如获取输入值或元素属性),则正常行为是返回调用它的同一对象。这样,而不必说SomeObject。反复地,您可以简单地在前一个方法的返回值上调用next方法。因此,链式方法等同于您以前看到的代码。