在库中实现Javascript链接的最佳方法 [英] Best way to implement Javascript chaining in a library

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

问题描述

我正在创建一个JavaScript库。我一直在尝试实施链接。

I'm creating a JavaScript library. I've been trying to implement chaining.

0:我刚开始提出的内容:

function V(p) {
  return {
    add : function(addend) { return V(p + addend); },
    sub : function(subtra) { return V(p - subtra); },
  };
}

使用这种方法我可以轻松链接:

Using this method I can chain easily:

V(3).add(7).sub(5) // V(5)

不幸的是,结果始终是一个包装的V()函数,我无法以这种方式提取结果值。所以我想了一下这个问题并提出了两个半解决方案。

Unfortunately the result is always a wrapped V() function, I am unable to extract the resulting value this way. So I thought about this problem a bit and came up with two semi-solutions.

1:将旗帜传递给最后一个方法

function V(p, flag) {
  if(flag)
    return p;
  else
    return {
      add : function(addend, flag) { return V(p + addend, flag); },
      sub : function(subtra, flag) { return V(p - subtra, flag); }
    };
}

使用此方法我可以通过将标志传递给最后一个方法来结束链我使用:

Using this method I can end the chain by passing a flag to the last method I use:

V(3).add(7).sub(5, true) // 5

虽然这很好用,但它需要重复一些代码并使链接的可读性降低,而且代码不那么优雅。

While this works just fine, it requires some code repetition and makes chaining less readable and my code less elegant.

2:使用start()和end()方法

_chain = false;
function V(p) {
  function Wrap(w) {
    return (_chain) ? V(w) : w;
  }
  return {
    add : function(addend) { return Wrap(p + addend); },
    sub : function(subtra) { return Wrap(p - subtra); },
    start : function() { _chain = true; },
    end : function() { _chain = false; return p; }
  };
}

使用此方法,您可以执行单个操作,而无需更多代码:

Using this method you can do single operations with no more code:

V(3).add(7) // 10

但链接需要两种方法,使得可读性低得多:

But chaining requires two more methods, making things a lot less readable:

V(3).start().add(7).sub(5).end() // 5

所以基本上我只是在寻找实现链接到我的库的最佳方法。理想情况下,我正在寻找可以使用任意数量方法的东西,而不需要以不优雅的方式终止链。

So basically I'm just searching for the best way to implement chaining into my library. Ideally I'm looking for something where I can use any number of methods and don't need to terminate the chain in inelegant ways.

V(3).add(7).sub(5) // 5, perfect chaining


推荐答案

为什么不引入私有变量并进行处理?我想这更方便。另外,拥有一个最终返回计算值的纯getter可能是一个好主意。这可能如下所示:

Why not introducing a private variable and working on that ? I guess that is even more convinient. Plus it's probably a good idea to have a pure "getter" which finally returns the computed value. This could look like:

function V(p) {
    var value = p;

    return {
        add : function(addend) { value += addend; return this; },
        sub : function(subtra) { value -= subtra; return this; },
        get : function() { return value; }
    };
}

V(5).add(7).sub(5).get();  // 5

您无法返回对象显然在 getter函数中。所以你需要一些方法来链接结束并返回一个值。

You cannot return the Object in a getter function obviously. So you need some method where the chaining ends and returns a value.

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

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