简体   繁体   English

如何重复调用同一函数?

[英]How to call the same function repeatedly?

I want to call a function twice but not through the traditional way. 我想两次调用一个函数,但不是通过传统方式。 A quick example of what I would be looking to do is below: 下面是我想要做的一个简单示例:

    var myfunc = {
        copy: function(message){
            console.log(message);
        }
    }

    myfunc.copy('hello').copy('world');

    // result 'hello'
    // result 'world'

Is this even possible? 这有可能吗?

Yes, but you should return the correct object: 是的,但是您应该返回正确的对象:

var myfunc = {
    copy: function(message){
        console.log(message);
        return this;
    }
};
myfunc.copy('hello').copy('world'); 
// hello
// world

This technique is also known as Method chaining . 此技术也称为方法链接

No, this will fail because .copy() doesn't return anything, so the second .copy() would throw an undefined error. 不,这将失败,因为.copy()不返回任何内容,因此第二个.copy()会引发未定义的错误。

Try this: 尝试这个:

var myfunc = {
    copy: function(message){
        console.log(message); 
        return this;
    }
}

It's called method chaining. 这称为方法链接。 Here's a blog that talks about this that you should be able to read and use to answer your question. 这是一个讨论此问题的博客,您应该能够阅读并用来回答问题。

Basically you will need to use return this; 基本上,您将需要使用return this; to return the current object so the next method can use it. 返回当前对象,以便下一个方法可以使用它。

// define the class
var Kitten = function() {
  this.name = 'Garfield';
  this.color = 'brown';
  this.gender = 'male';
};

Kitten.prototype.setName = function(name) {
  this.name = name;
  return this;
};

Method chaining in JavaScript JavaScript中的方法链接

You need to chain this, You can also look up to jquerys $.fn how they create method chaining simply return this as this object is your myFunc variable and is used again by the next function 您需要对此进行链接,您还可以查询jquery $ .fn它们如何创建方法链接,只需将其返回即可,因为此对象是您的myFunc变量,并由下一个函数再次使用

var myfunc = {
   copy: function(message){
       console.log(message);
       return this; 
   }
};
myfunc.copy('hello').copy('world');

As other answers have pointed out, you need to return this in order to be able to call the same or additional functions on the same object. 正如其他答案所指出的那样,您需要返回this ,以便能够在同一对象上调用相同或其他函数。

You can do this a bit more easily (?) via a higher-order function which we will call "chainify", which takes care of returning this for you: 您可以通过一个高阶函数(称为“ chainify”)更轻松地完成此操作(?),该函数将为您返回this值:

function chainify(fn) {
  return function() {
    fn.apply(this, arguments);
    return this;
  };
}

You can chainify your object methods in various ways, but here's one: 您可以通过多种方式链接对象方法,但这是一种:

var myfunc = {
  init: function() {
    this.copy = chainify(this.copy);
    return this;
  },

  copy: function(message){
      console.log(message);
  }
}.init();

This has the minor advantage that each and every method does not need to be cluttered with the return this at the end, and you don't run the risk of forgetting to do so. 这具有一个次要的优点,即每个方法都不需要在return this的末尾杂乱无章,并且您不必冒这样做的风险。

This is known a Builder design pattern where in you cascade methods. 这是一个已知的Builder设计模式,您可以在其中层叠方法。 Google it if it helps. 谷歌它,如果有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM