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
的语法以及何时使用它。
dojo.partial (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
允许您添加任意数量的参数,这些参数可能被推到签名的前面,而不仅仅是一个。
功能部分 (Function.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!
dojo.xd.js