dojo.xd.js_dojo.partial和MooTools的Function.partial令您赞叹不已

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:

请注意,该函数需要两个参数: nodecontent 。 这是一个简单的通用函数,可以在任何地方使用并且可以由许多不同的函数使用,对吗? 现在让我们说我正在打电话给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方法的签名为( contentioArgs )。 要将我的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.partialFunction.partial完全是FTW!

翻译自: https://davidwalsh.name/dojo-partial

dojo.xd.js

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值