dojo.xd.js
Much like MooTools, the Dojo Toolkit features a host of JavaScript language helpers. One of those helpers is dojo.partial. This method, which lives in Dojo Base, allows you to call a method with additional arguments appended to the front of a function signature. Sound a bit weird? It did to me too. Let's take a quick peek at dojo.partial's syntax and when you'd use it.
与MooTools一样,Dojo Toolkit具有许多JavaScript语言帮助程序。 这些助手之一是dojo.partial 。 此方法位于Dojo Base中,它允许您调用在函数签名的前面附加了其他参数的方法。 听起来有点怪吗? 对我也是如此。 让我们快速浏览一下dojo.partial的语法以及何时使用它。
Let's say you have a function whose main purpose is to place content into a node:
假设您有一个函数,其主要目的是将内容放入节点:
// A sample function which could use partial function placeContent(node, content) { node.innerHTML = content; }Note that the function expects two arguments: node and content. This is a simple, general purpose function that could be used anywhere and by many different functions, right? Now let's say that I'm making a xhrGet call:
请注意,该函数需要两个参数: node和content 。 这是一个简单的通用函数,可以在任何地方使用并且可以由许多不同的函数使用,对吗? 现在让我们说我正在打电话给xhrGet :
dojo.xhrGet({ url: "content.html", load: function(content, ioArgs) { } });The signature of the load method is (content, ioArgs). To use my placeContent function with the load handler, you'd have to code:
load方法的签名为( content , ioArgs )。 要将我的placeContent函数与load处理程序一起使用,您必须编写以下代码:
dojo.xhrGet({ url: "content.html", load: function(content, ioArgs) { placeContent("myNode", content); } });That's not the worst thing in the world, but it's a bit...meh. Using dojo.partial, we could instead code:
这不是世界上最糟糕的事情,但这有点...嗯。 使用dojo.partial ,我们可以改为编写代码:
dojo.xhrGet({ url: "content.html", load: dojo.partial(placeContent, "myNode") });Even though the first argument of the load callback signature is the content, the dojo.partial call shifts the provided arguments to the front of the argument list, thus placing the node argument before the content argument when used with placeContent. dojo.partial allows us to avoid using "wrapping" functions to add an argument to the arguments array. dojo.partial allows you to add any number of arguments which may be pushed to the front of the signature, not just one.
即使load回调签名的第一个参数是content, dojo.partial调用也将提供的参数移到argument列表的最前面,因此当与placeContent使用时,将node参数放置在content参数之前。 dojo.partial允许我们避免使用“包装”函数将参数添加到arguments数组。 dojo.partial允许您添加任意数量的参数,这些参数可能被推到签名的前面,而不仅仅是一个。
I've taken a quick moment to duplicate the dojo.partial function for MooTools:
我花了一些时间来复制MooTools的dojo.partial函数:
// The implementation Function.implement("partial", function(/* all args */) { var self = this, args = Array.from(arguments); return function() { self.apply(this, args.append(arguments)); }; });An example usage would look like:
用法示例如下所示:
new Request({ url: "partial.html", //onComplete: myFn.partial("myNode").bind(this) onComplete: placeContent.partial("myNode") }).send();Just as easy to use as Dojo's method and just as useful. I love that this method allows you to skip writing one-line callback wrappers and allow you to keep your utility function signatures the way they are. dojo.partial and Function.partial are fully FTW!
与Dojo的方法一样易于使用,也非常有用。 我喜欢这种方法可以让你跳过写一个回调的包装,让你保持你的效用函数签名会是这样的。 dojo.partial和Function.partial完全是FTW!
翻译自: https://davidwalsh.name/dojo-partial
dojo.xd.js
相关资源:jdk-8u281-windows-x64.exe