子方法的方法链接 [英] method chaining with sub-methods

查看:83
本文介绍了子方法的方法链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将方法链与子方法一起使用.

I am trying to use method chain with sub-methods.

IE:foo("bar").do.stuff()

要捕获的stuff()需要引用bar("bar")的值

是否有任何this.callee或其他此类参考文献来实现这一目标?

Is there any this.callee or other such reference to achieve this?

推荐答案

是否有this.callee或其他类似的引用来实现这一目标?

Is there any this.callee or other such reference to achieve this?

否,您必须让foo返回带有do属性的对象,该对象可以是:

No, you'd have to have foo return an object with a do property on it, which either:

  1. 在对foo

将来自foo("bar")的信息作为do的属性,然后通过this

Have information you want from foo("bar") as a property of do, and then reference that information in stuff from the do object via this, or

// 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>

这篇关于子方法的方法链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆