if (!Function.prototype.bind) { ?Function.prototype.bind = function (oThis) { ? ?if (typeof this !== "function") { ? ? ?// closest thing possible to the ECMAScript 5 ? ? ?// internal IsCallable function ? ? ?throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable") ? ?}
2018-08-09
jquery的事件綁定方法.bind(),和視頻所講的js原生修改函數(shù)this指向的.bind(),是兩個(gè)東西啊。大兄弟,你會(huì)不會(huì)把這兩者混淆了~
2017-08-29
bind?函數(shù)在 ECMA-262 第五版才被加入;它可能無法在所有瀏覽器上運(yùn)行。你可以部份地在腳本開頭加入以下代碼,就能使它運(yùn)作,讓不支持的瀏覽器也能使用 bind() 功能。
if (!Function.prototype.bind) {
?Function.prototype.bind = function (oThis) {
? ?if (typeof this !== "function") {
? ? ?// closest thing possible to the ECMAScript 5
? ? ?// internal IsCallable function
? ? ?throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")
? ?}
? ?var aArgs = Array.prototype.slice.call(arguments, 1),
? ? ?fToBind = this,
? ? ?fNOP = function () {},
? ? ?fBound = function () {
? ? ? ?fBound.prototype = this instanceof fNOP ? new fNOP() : fBound.prototype ? ? ? ?return fToBind.apply(this instanceof fNOP
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? this
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? : oThis || this,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? aArgs.concat(Array.prototype.slice.call(arguments)))
? ? ? ?}
? ?if( this.prototype ) {
? ? ?// Function.prototype doesn't have a prototype property
? ? ?fNOP.prototype = this.prototype ? ?}
? ?return fBound ?}}