最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

extjs - Javascript bind() vs createDelegate() - Stack Overflow

programmeradmin3浏览0评论

Imagine I want to pass a member function as a callback.

What should I use to pass a context - bind() or createDelegate()?

I mean, this:

someObj.on('someEvent', this.someMethod.createDelegate(this));

Or this:

someObj.on('someEvent', this.someMethod.bind(this));

Imagine I want to pass a member function as a callback.

What should I use to pass a context - bind() or createDelegate()?

I mean, this:

someObj.on('someEvent', this.someMethod.createDelegate(this));

Or this:

someObj.on('someEvent', this.someMethod.bind(this));
Share Improve this question edited Feb 26, 2015 at 21:25 ᄂ ᄀ 5,7927 gold badges48 silver badges60 bronze badges asked Nov 20, 2013 at 8:53 MnZrKMnZrK 1,3801 gold badge12 silver badges24 bronze badges 4
  • What framework are you working with? Do you mean in jQuery? – V G Commented Nov 20, 2013 at 8:57
  • 1 createDelegate is not a native JavaScript method. – David Hellsing Commented Nov 20, 2013 at 8:57
  • Depends on what createDelegate does. – Felix Kling Commented Nov 20, 2013 at 8:58
  • If you or any visitor are interested in Function.createDelagate from AJAX.NET, refer to this this verbose post: stackoverflow./questions/881544/… – egidiocs Commented Mar 8, 2016 at 21:06
Add a ment  | 

3 Answers 3

Reset to default 3

The bind function is a recent addition to ECMA-262(Javascript), 5th edition;

The createDelegate is not a native JavaScript method in any edition.

So better to use bind. And use polyfill for browser where it not implemented.

P.S. If you use any popular framework, probably it has such methods. For example jQuery has $.proxy static method that do the same.

Assuming you are using an old version of ExtJS (3.4.0 for example), there is little difference in what bind and createDelegate do. The big difference es from the fact that bind is not available for older browsers.

One thing to note is that createDelegate is not available in newer versions of ExtJS, as the framework has moved away from the idea of changing native object prototypes. It has been replaced by Ext.Function.bind.

*) There is some difference between the native Function.prototype.bind and Ext.Function.bind, in how the arguments bound to the function are treated. It doesn't look like it affects your code, though. Read the documentation to see the exact difference.

MDN link for Function.prototype.bind:

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

ExtJS 3.4.0 link for Function.prototype.createDelegate (added by ExtJS)

http://docs.sencha./extjs/3.4.0/source/Ext.html#Function-method-createDelegate

ExtJS 4.0.7 link for Ext.Function.bind

http://docs-origin.sencha./extjs/4.0.7/source/Function2.html#Ext-Function-method-bind

If you talk about jQuery, look at the documentation (second parameter eventData)

someObj.on('someEvent', {me: this}, this.someMethod);

someObj:

{

    someMethod: function(eventData){
      var me = eventData.me;
    }

}
发布评论

评论列表(0)

  1. 暂无评论