如何在没有链接的情况下调用函数链中的任何函数?

发布于 2024-11-14 02:43:11 字数 352 浏览 7 评论 0原文

抱歉,如果我的问题不够清楚。我将把我的代码放在这里...

var chain = {
    'fn_1' : {
             //fn_1 code here
             chain.fn_2();},
    'fn_2' : {
             //fn_2 code here
             chain.fn_3();}

...and so on
}

假设我想调用 chain.fn_1(),有没有办法可以在不调用 chain.fn_2() 的情况下做到这一点?

我现在能想到的是一个标志,但是每个函数可能都会有很多多余的标志。你们有什么想法吗?

Sorry if my question wasn't clear enough. I'll put my code here...

var chain = {
    'fn_1' : {
             //fn_1 code here
             chain.fn_2();},
    'fn_2' : {
             //fn_2 code here
             chain.fn_3();}

...and so on
}

Let's say if i wana call chain.fn_1(), is there a way I can do that without calling chain.fn_2()?

What I can think of right now is a flag, but that would be alot of excess flags probably for each function. Do you guys have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女 2024-11-21 02:43:11

如果这一系列函数每个都调用下一个函数,那么您是正确的,那么您需要某种标志。很可能,最好的方法是修改您的函数,以便它们返回对对象的引用。然后你可以像这样链接:

var chain = {
  'fn_1': function () {
    // do something here.
    return this;
  },
  'fn_2': function () {
    // do something here.
    return this;
  },
  'fn_3': function () {
    // do something here.
    return this;
  }
};

// call the full chain:
chain.fn_1().fn_2().fn_3();

// call only the middle.
chain.fn_2();

If the series of functions each call the next one you're correct, you'd need to have some sort of flag. In all likelihood, what would be best would be to modify your functions so that they return the reference to the object. Then you could chain like so:

var chain = {
  'fn_1': function () {
    // do something here.
    return this;
  },
  'fn_2': function () {
    // do something here.
    return this;
  },
  'fn_3': function () {
    // do something here.
    return this;
  }
};

// call the full chain:
chain.fn_1().fn_2().fn_3();

// call only the middle.
chain.fn_2();
小伙你站住 2024-11-21 02:43:11

gddc 的答案是最好的,但是如果您由于某种原因无法修改对象,您可以这样做:

var _oldFn2 = chain.fn_2
chain.fn_2 = function() { return; };
chain.fn_1();
chain.fn_2 = _oldFn2;

g.d.d.c's answer is best, but if you can't modify the object for some reason, you could do this:

var _oldFn2 = chain.fn_2
chain.fn_2 = function() { return; };
chain.fn_1();
chain.fn_2 = _oldFn2;
栀子花开つ 2024-11-21 02:43:11
var chain = {
    fn : ['fn1', 'fn2', 'fn3'],
    call : function(name) {
       var i = 0, pos = -1, l = this.fn.length;
        for(i = 0; i < l; i += 1) {
            if(this.fn[i] == name) {
                pos = i;
            }
            if(pos !== -1) {
                this[this.fn[i]]();             
            }
        }

    },
    fn1 : function() {
        alert('fn1');
    },
    fn2 : function() {
        alert('fn2');
    },
};
chain.call('fn1'); //chain
chain.fn1(); //single
var chain = {
    fn : ['fn1', 'fn2', 'fn3'],
    call : function(name) {
       var i = 0, pos = -1, l = this.fn.length;
        for(i = 0; i < l; i += 1) {
            if(this.fn[i] == name) {
                pos = i;
            }
            if(pos !== -1) {
                this[this.fn[i]]();             
            }
        }

    },
    fn1 : function() {
        alert('fn1');
    },
    fn2 : function() {
        alert('fn2');
    },
};
chain.call('fn1'); //chain
chain.fn1(); //single
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文