javascript 函数链接,链成员无需相互了解
我想创建一个 JavaScript 管道,如 powershell、bash (|) 或 f# (|>)。 IE。相当于
getstuff() | sort() | grep("foo") | take(5)
我在咖啡脚本论坛上看到了关于此的讨论,但最终他们搁置了它,因为每个人都说你可以通过函数链做同样的事情。但据我所知,requires getstuff 返回的东西有一个排序方法; sort 方法必须返回带有 grep 方法的内容等。这是相当严格的,因为它要求所有潜在的管道成员提前相互了解。我知道 JavaScript 有一些非常聪明的技巧,而我仍然处于 101 级别 - 那么
getstuff().sort.().grep().take()
如果没有这个限制,这是否可行
I want to create a javascript pipeline like powershell, bash (|) or f# (|>). Ie. something equivalent to
getstuff() | sort() | grep("foo") | take(5)
I saw a discussion about this in coffeescript forum but in the end they shelved it because everybody said that you could do the same thing with function chaining. But as far as I can see that requires getstuff returns something that has a sort method on it; the sort method must return something that has grep method on it etc. This is pretty restrictive as it requires all potential pipeline members to know about each other in advance. I know JavaScript has some pretty clever tricks in it and I am still at the 101 level - so is this doable
getstuff().sort.().grep().take()
without that constraint
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
?
在高层次上,你可以做一些类似于 jQuery 在幕后所做的事情来允许链接。创建一个类似数组的包装对象类型,其中包含您希望能够调用的所有函数;除了显式传递的参数之外,每个连续的链式调用还可以在内部堆栈上进行操作。
不是要继续打败 jQuery 的死马,而是理解我所说的最好方法之一就是开始挖掘 jQuery 核心源代码 并弄清楚链接是如何工作的。
No.
At a high level, you could do something similar to what jQuery does under the hood to allow chaining. Create an array-like wrapper object type which has all of the functions you want to be able to call; each successive chained call can operate on an internal stack in addition to the explicitly-passed arguments.
Not to keep beating the dead jQuery horse, but one of the best ways to understand what I'm talking about is to just start digging through the jQuery core source code and figure out how the chaining works.
定义一个对象来支持您想要的函数链接类型实际上非常简单:
这就是您制作
getstuff().sort.().grep('foo').take(5) 所需的全部内容
工作。Defining an object to support the kind of function chaining you want is actually quite easy:
That's all you need to make
getstuff().sort.().grep('foo').take(5)
work.您可以进行这些调用,而不必担心返回值具有适当的方法,如下所示:
但是,这并不能解决每个函数都需要传递对其有意义的数据的问题。即使 JavaScript 也没有那么狡猾。例如,您可以对图像调用 sort(),但没有有意义的方法来生成结果。
You can make those calls without worrying about the return values having the appropriate methods like so:
But, that doesn't get through the problem of each function needing to be passed data that is meaningful to it. Even JavaScript isn't that slippery. You can call sort() on an image (for example,) but there's no meaningful way to generate results.
您可以通过返回一个特殊对象来执行类似的操作,该对象具有所有必需的方法,但可以用来代替最终值。例如,您可以返回一个包含所有这些方法的 Array 实例。
请注意,上面的实现并不是特别快,但它通过仍然保持全局
Allay
原型干净的方式返回具有特殊方法的数组,因此不会干扰其他代码。您可以通过在 Array.prototype 上定义这些特殊方法来加快速度,但您应该小心...
或者,如果您的浏览器支持子类化 Array,那么您所需要的只是一个超类和一个方便的构造函数
getstuff()
。You could do something similar by returning a special object that has all required methods on it, but can be used instead of the final value. For example, you could return an
Array
instance that has all these methods on it.Note that the above is not a particularly fast implementation, but it returns arrays with special methods by still keeping the global
Allay
's prototype clean, so it won't interfere with other code.You could make it faster by defining these special methods on the
Array.prototype
, but you should be careful with that...Or, if your browser supports subclassing
Array
, then all you need is a supclass and a handy constructor,getstuff()
.