2010-01-09 69 views
16

原谅我的无知,因为我不熟悉的jQuery。是否有相当于dojo.hitch()?它返回一个保证在给定范围内执行的函数。jquery是否有相当于dojo.hitch()?

- 编辑 - 按照要求,这里有一个例子。我非常频繁地使用连接来确保回调在正确的对象内执行。例如,假设我有一个名为doSomethingAsync的实用程序方法,并将其传递给回调函数。随着顺利,我可以确保该功能即使在实用方法执行Ajax调用等特定范围内的执行:


expectedScopeObj = { 
    flag: true, 
    callback: function(){console.debug(this.flag);}, 
    main: function() { 
    // without hitch the callback function would not find flag 
    core.util.doSomethingAsync(dojo.hitch(this, this.callback)); 
    } 
} 

不顺利,回调函数可能被不同的范围和执行this.flag未定义时会引发错误。但是,如果遇到困难,保证在execptedScopeObj之内执行。

+0

究竟是你想实现什么目标?示例代码将是有益的 – Ariel 2010-01-09 22:41:35

+2

不应该的例子已经dojo.hitch(这个,这个,回调)? – Murali 2010-10-25 01:19:36

+0

此[希契](https://github.com/phiggins42/bloody-jquery-plugins/blob/master/hitch。js)jQuery插件改编自Dojo – PHearst 2013-07-01 22:26:12

回答

44

我知道这已被回答,但不正确。 jQuery.proxy是你在找我相信。

UPDATE

很多很多年以后大量的JavaScript的工作后,剥了我的jQuery的使用,因为浏览器的兼容性已经不再成为一个问题后,我会建议使用Function.prototype.bindjQuery.proxy,为@bobince suggested 。虽然这仍然是对原始问题的正确答案,但我觉得有义务将人们引导到一个香草解决方案,而不是依靠jQuery。

2

不,不是在1.3.2,至少,我不知道1.4。有,但是,一些plugins

(function($) { 
    $.fn.hitch = function(ev, fn, scope) { 
    return this.bind(ev, function() { 
     return fn.apply(scope || this, Array.prototype.slice.call(arguments)); 
    }); 
    }; 
})(jQuery); 
+0

我们尽量避免使用插件,因为我们更喜欢使用基本库的支持。它看起来像现在我们最好的赌注是使用合并的dojo基础和jquery构建,所以我们得到两全其美:) – Lightbeard 2010-01-13 05:55:52

+0

这是死灵法领土,但对于尚未解决此问题的任何其他人,您可能会看看@phiggins'hitch插件:https://github.com/phiggins42/bloody-jquery-plugins/blob/master/hitch.js – 2011-10-04 21:37:07

+0

更僵尸般的,我只是转贴 – PHearst 2013-07-01 22:28:00

1

通过bobince提到的function.bind是一个非常有用的工具。你可以用它来重写挂钩函数倒也干脆:

// The .bind method from Prototype.js 
if (!Function.prototype.bind) { // check if native implementation available 
    Function.prototype.bind = function(){ 
    var fn = this, args = Array.prototype.slice.call(arguments), 
     object = args.shift(); 
    return function(){ 
     return fn.apply(object, 
     args.concat(Array.prototype.slice.call(arguments))); 
    }; 
    }; 
} 

jQuery.hitch = function(scope, fn) { 
    if (typeof fn == "string") fn = scope[fn]; 
    if (fn && fn.bind) return fn.bind(scope); 
}; 

至少根据您链接的文档页面上,这是我看到了功能的工作...

6

[ADMIN编辑:请注意更受欢迎的答案,下面.- danorton]

我会去function.bind,这将是在未来的JavaScript版本中执行此操作的标准方式。除了修复this之外,它还允许您将参数传递给目标函数。

直到所有的浏览器都支持它本身,你可以hack support in yourself

+0

以上的链接,虽然绑定是相似的,当然非常有用,它不完成任务 – Lightbeard 2010-01-13 05:53:00

+0

Erm ...它完成了适当地调用'this'设置的函数的任务。你还希望'hitch'做什么?上面的示例代码完全不起作用。 – bobince 2010-01-13 12:46:16

+0

你说得对。我误解了,并认为你指的是jQuery的绑定功能。谢谢 – Lightbeard 2010-01-13 13:35:39

1
//why not just: 
(function($){ 
    $.hitch = function(scope, fn){ 
    return function(){ 
     return fn.apply(scope, arguments); 
    } 
    } 
})(JQuery); 
//This works exactly like Dojo's hitch from what I can tell. 
0

在我的脑海里,hitch在Dojo是比较复杂的,

例如,

function f(a,b,c){return a+b*c} 
var newF = hitch(null,f,1,undefined,3); 
newF(2) /*1+2*3*/ 
0
/** 
* This is for simulate dojo.hitch. 
* @param $ 
*/ 
(function($) { 
    $.hitch = function(context, func) { 
     var args = Array.prototype.slice.call(arguments, 
       2/*Remove context, and func*/); 

     return function() { 
      return func.apply(context, 
        Array.prototype.concat.call(args, arguments)); 
     }; 
    }; 
})(jQuery); 
相关问题