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

javascript - Is there a risk involved in using MDN bind polyfill? - Stack Overflow

programmeradmin2浏览0评论

I have developed a Javascript library which requires the bind method. Unfortunately, bind is not supported by IE8.

There are a polyfill on the MDN website which work well.

My question is: Are there problems or possible inpatibility between this polyfill and other Javascript libraries ?

It is safe to use in any case?

I have developed a Javascript library which requires the bind method. Unfortunately, bind is not supported by IE8.

There are a polyfill on the MDN website which work well.

My question is: Are there problems or possible inpatibility between this polyfill and other Javascript libraries ?

It is safe to use in any case?

Share edited Mar 18, 2013 at 13:59 rbedger 1,24510 silver badges20 bronze badges asked Mar 18, 2013 at 13:53 BAKBAK 1,0059 silver badges23 bronze badges 3
  • did you try the patibility code they provide on the mdn page? developer.mozilla/en-US/docs/JavaScript/Reference/… – Sebas Commented Mar 18, 2013 at 14:15
  • Yes, and it seem to work fine. But can i be sure that it the case in any circumstance ? – BAK Commented Mar 18, 2013 at 14:17
  • Not ANY circumstance but most. – backdesk Commented Dec 18, 2013 at 13:07
Add a ment  | 

2 Answers 2

Reset to default 6

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

For me most obvious differences with native bind are:

  • arguments.caller does not point to the caller of bound function, but you shouldn't use it anyway
  • length of bound function is set to 0, this may affect function arity checks like https://github./fitzgen/wu.js/blob/master/lib/wu.js#L406

IMHO if you are using only "the good parts" of JavaScript, and not developing core of some framework (for IE8?), you shouldn't face any problems with this polyfill.

The answer is pretty much there on the MDN page itself:

"If you choose to use this partial implementation, you must not rely on those cases where behavior deviates from ECMA-262, 5th edition! With some care, however (and perhaps with additional modification to suit specific needs), this partial implementation may be a reasonable bridge to the time when bind() is widely implemented according to the specification."

There's nothing wrong with the MDN shim as such. However if you choose to use their shim make sure that it can't be overridden by other libraries. I had an issue a while ago with Strophe doing just that and replacing one shim with another.

I tend to use underscore to cover stuff like this but there are other options like es5shim. With underscore you have a method called (you guessed it) '.bind' and works slightly differently to MDN's shim (uses 'new' invocation). Underscore also has a great method called '.partial' which can be useful in scenarios where you don't want to change the value of 'this' but partially apply arguments.

The point I am making here is that instead of shimming, maybe look at something that is properly protected/encapsulated within a library. The chances are you're going to need more than one shim in any case if you're targeting browsers like IE8.

Lastly, and not so importantly check out the performance tests at: http://jsperf./browser-vs-es5-shim-vs-mdn-shim

发布评论

评论列表(0)

  1. 暂无评论