在没有目标的方法上可以使用dojo方面吗? [英] is it possible to use dojo aspect on a method with no target?
问题描述
我有一个模块如下:
define([...], function(...){
function anothermethod() {...}
function request() {....}
request.anothermethod = anothermethod;
return request;
}
现在我想在 anothermethod上使用dojo方面
方法的请求方法(关闭)可以吗?如果是这样,我应该放在目标参数中?
Now i want to use dojo aspect before on the anothermethod
method of the request method (closure). is it possible? If so, what should i put in the target parameter?
aspect.before(target, methodName, advisingFunction);
另一个方法不是直接调用的请求方法被调用,间接调用anothermethod:
And the anothermethod is not called directly. First the request method is called which calls the anothermethod indirectly:
require(['dojo/aspect'], function(aspect){
function anothermethod() {
console.log('another method');
}
function beforeanothermethod() {
console.log('before another method');
}
function request() {
anothermethod();
}
request.anothermethod = anothermethod;
aspect.before(request, 'anothermethod', beforeanothermethod);
request();
})
https://jsfiddle.net/ahwgw5tb/1/
推荐答案
在您的情况下,您已经有一个目标,即请求
:
In your case, you already have a target, being request
:
aspect.before(request, 'anothermethod', function() {
// Do something
});
但是要回答你的原始问题,不,你不能。你总是需要一个目标。在正常功能的情况下,目标将是该功能所在的本地范围,但无法访问。
But to answer your original question, no you can't. You always need a target. The target in case of a normal function, would be the local scope where that function lives in, but there's no way to access that.
所以,最好的解决方案是将它添加到特定对象(请求
)像您一样。
So, the best solution is to add it to a specific object (request
) like you did.
这篇关于在没有目标的方法上可以使用dojo方面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!