带有子方法的方法链接
method chaining with sub-methods
我试图使用子方法方法链。
IE: foo("bar").do.stuff()
问题是stuff()
需要引用bar("bar")
的值
是否有this.callee
或其他类似的参考来实现这一点?
有这个吗?Callee或其他类似的参考来实现这一点?
不,你必须让foo
返回一个具有do
属性的对象,它要么:
-
使
stuff
成为对foo
调用的闭包 -
将
foo("bar")
中的信息作为do
的属性,然后通过this
从do
对象中引用stuff
中的信息,或者
// Closure example:
function foo1(arg) {
return {
do: {
stuff: function() {
snippet.log("The argument to foo1 was: " + arg);
}
}
};
}
foo1("bar").do.stuff();
// Using the `do` object example (the `Do` constructor and prototype are just
// to highlight that `stuff` need not be a closure):
function Do(arg) {
this.arg = arg;
}
Do.prototype.stuff = function() {
snippet.log("The argument to foo2 was: " + this.arg);
};
function foo2(arg) {
return {
do: new Do(arg)
};
}
foo2("bar").do.stuff();
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
尝试将do
, stuff
设置为foo
的属性,在stuff
返回传递给foo
的参数,从foo
返回this
var foo = function foo(args) {
this.stuff = function() {
return args
}
this.do = this;
return this
}
console.log(foo("bar").do.stuff())
相关文章:
- 简单的JavaScript方法链接
- D3使用方法链接时的不同行为
- Javascript方法链接定义
- 使用javascript的方法链接不起作用
- 如何在JS中完成方法链接
- 缺点和替代方案 - JS方法链接
- jQuery/JS方法链接
- 是否可以在JavaScript中的变量名后面的新行上开始方法链接
- CoffeeScript调用返回对象的方法(链接)
- 方法链接返回未定义
- 如何在维护命名空间的同时创建类似jQuery的方法链接
- jQuery如何进行方法链接
- 如何有效地调试方法链接函数参数
- JavaScript 方法链接和 this 对象
- NODEJS中的Javascript方法链接
- 带有子方法的方法链接
- 在方法链接中使用函数调用者名称
- 在CouchDB中使用Map-Reduce方法链接文档
- 对象或方法链接如何在jQuery中工作
- 多元素选择器上的jQuery方法链接