24

Ich schreibe ein bisschen JavaScript, das die Object.bind Methode verwendet.Wie man mit dem Mangel an JavaScript arbeitet Object.bind() - Methode in IE 8

funcabc = function(x, y, z){ 
    this.myx = x; 
    this.playUB = function(w) { 
     if (this.myx === null) { 
      // do blah blah 
      return; 
     } 

     // do other stuff 
    }; 
    this.play = this.playUB.bind(this); 
}; 

Da ich in WinXP mit Firefox und manchmal Test in Win7 mit IE 9 oder 10 entwickeln, ich habe bemerken oder nicht die Aufmerksamkeit auf die Tatsache, dass IE8 und unten nicht bind unterstützen.

Dieses spezielle Skript verwendet nicht die Leinwand, also bin ich ein wenig zögerlich, alle IE 8 Benutzer abzuschreiben.

Gibt es eine Standardumgehung?

Ich komme irgendwie in Ordnung in JavaScript, aber ich bin immer noch ein bisschen wie ein Noob. Also vergib mir, wenn die Lösung völlig offensichtlich ist.

+0

@micha, Ja, fehlt: 'Nicht in dem folgende Dokument Modi unterstützt : Quirks, Standards des Internet Explorer 6, Internet Explorer 7 Standards, Internet Explorer 8 Standards. – Alexander

Antwort

49

Es gibt eine gute Kompatibilität Skript auf dieser Seite: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

einfach kopieren und in Ihr Skript einfügen.

EDIT: Platzieren Sie das Skript zur Verdeutlichung.

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() { 
      return fToBind.apply(this instanceof fNOP && oThis 
       ? this 
       : oThis, 
       aArgs.concat(Array.prototype.slice.call(arguments))); 
     }; 

    fNOP.prototype = this.prototype; 
    fBound.prototype = new fNOP(); 

    return fBound; 
    }; 
} 
+2

Das funktionierte wie ein Charme.Eine Fehlerbehebung für das Bindeproblem gefunden und gelernt, in den Mozilla-Dokumenten nach dem Schlüsselwort Kompatibilität zu suchen. – Claude

+0

Übrigens, IE 8 hat zu viele fehlende Funktionen. Grundsätzlich muss ich bei HTML5-kompatiblen Browsern bleiben. Ohne den handlichen Dandy Audio() gibt es keinen Sinn. – Claude

+0

@alex - Weißt du, ob IE10 Bind unterstützt oder benötigt es die Work-around, die du erwähnt hast? –

4

Die beste Lösung könnte sein, Modernizr zu installieren.

Modernizr sagt Ihnen, ob der aktuelle Browser diese Funktion nativ implementiert hat oder nicht und es bietet einen Skript-Lader, so dass Sie Polyfills zum Auffüllen der Funktionalität in alten Browsern ziehen können.

Hier ist der Link Ihre modernizr angepasste Version zu generieren:
http://modernizr.com/download/#-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes

+0

Wie überprüft man, ob der aktuelle Browser diese Funktion nativ implementiert hat oder nicht mit Modernizer? – dragonfly

+3

Diese Antwort ist auf einmal total falsch und einigermaßen korrekt! Es ist völlig falsch, weil Modernizr keinen Test für die Verfügbarkeit der Funktionsbindung bietet (wahrscheinlich weil der Test so einfach ist, wie zu prüfen, ob 'Function.prototype.bind! == undefined'). Es ist jedoch etwas korrekt, da Modernizr tatsächlich eine Funktion enthält, die Polyfill selbst bindet! Details dazu finden Sie hier: https://github.com/Modernizr/Modernizr/issues/478 – WickyNilliams

+0

Modernizr> 3 enthält jetzt nicht mehr das Polyfill für bind – gunnx

0

Die Funktion Konstruktor ist die altmodische Art und Weise, dies zu tun:

var foo = function(x,y,z){ return Function("x,y,z","return Math.max.call(this, x, y, z)")(x,y,z) } 
 
    
 
var bar = function(x,y,z){ return Function("x,y,z","return Math.min.call(this, x, y, z)")(x,y,z) } 
 
    
 
console.log(foo(1,2,3)); 
 
    
 
console.log(bar(3,2,1));

Referenzen

2

Function.prototype.bind ist nicht in Internet Explorer 8 und unten unterstützt. Kompatibilitätstabelle hier: http://kangax.github.io/es5-compat-table/

Mozilla Developer Network bietet diese Alternative für älteren Browser, die nativ nicht implementiert .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() { 
      return fToBind.apply(this instanceof fNOP && oThis 
           ? this 
           : oThis, 
           aArgs.concat(Array.prototype.slice.call(arguments))); 
     }; 

    fNOP.prototype = this.prototype; 
    fBound.prototype = new fNOP(); 

    return fBound; 
    }; 
} 
Verwandte Themen