[英]java script call methods from another methods
I'm new in js.我是js新手。
I see code example:我看到代码示例:
foo.bar().baz()
How described foo bar and baz that we can call so?如何描述我们可以这样称呼的 foo bar 和 baz?
Thank you.谢谢你。
What are you are probably after is called chaining.你可能追求的是链接。 A method can return the object it's running on
this
, so that another method may be called.一个方法可以返回它在
this
上运行的 object,以便可以调用另一个方法。
var foo = {
bar: function() {
doStuff();
return this;
},
baz: function() {
doOtherStuff();
return this;
}
};
foo.bar().baz();
This is exactly how jQuery works, in order to allow things like:这正是 jQuery 的工作原理,以便允许以下操作:
$('#foo')
.html('<p>hi</p>')
.addClass('selected')
.css('font-size', '24px')
.show();
So let's say you had an object foo with two methods: bar and bad.所以假设你有一个 object foo 有两种方法:bar 和 bad。 The implementation of bar would be like this: function bar() { /* do work */ return this;
bar 的实现如下: function bar() { /* do work */ return this; } That returns foo itself so you can call baz since it's defined in foo.
这会返回 foo 本身,因此您可以调用 baz,因为它是在 foo 中定义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.