2009-07-27 10 views
2

Sorry, wenn dies beantwortet wurde, aber ich konnte eine Suche nach ihr nicht auftauchen ... eine schwierige Sache zu suchen, denke ich!Scoping "dies" in Javascript (und jQuery.extend)

Sagen, ich habe diese:

var MyPrototype = function() { this.init(); } 
$.extend(MyPrototype.prototype, { 
    a: 5, 
    init: function() { 
     var thing = new SomeOtherClass(); 
     thing.meth = this.meth; 
     // thing.meth() is called somewhere within thing; 
    }, 
    meth: function() { 
     alert(this.a); 
    } 
} 

Grundsätzlich ist mit einer anderen Klasse zu tun habe, die ihre eigenen Methoden, wie Rückrufe verwendet, z.B. Es wird erwartet, dass ich sie mit meiner eigenen Funktionalität überschreibe. Aber ich muss den richtigen Umfang von this dabei behalten (das einzige, was ich interessiere von SomeOtherClass ist, was an den Rückruf weitergegeben wird; nichts in dem Staat).

Wie Sie sich vorstellen können, funktioniert das nicht, weil thing keine a Eigenschaft hat! Ich bin nicht vertraut genug mit den Feinheiten von Javascript Scoping zu wissen, wie man this Bezug auf was ich will, obwohl!

+0

Bitte sehen: http://stackoverflow.com/questions/520019/controlling-the-value-of- this-in-a-jquery-event ... und auch: http://stackoverflow.com/questions/1043556/how-cani-i-keep-the-context-of-this-in-jquery ... and wirklich, die meisten Fragen in: http://StackOverflow.com/Questions/Tagged/this+javascript (das ist ein extrem häufiger Punkt der Verwirrung) – Shog9

Antwort

2

hier zwei weitere Antworten kombinierend, so dass Sie don‘ t müssen Ihre meth Funktion neu schreiben, würde ich dies tun:

var me = this; 
    thing.meth = function() { 
     MyPrototype.meth.apply(me, arguments); 
    }; 
-1

Wie wäre:

thing.meth.call(this); 

oder

thing.meth.apply(this); 

(Der einzige Unterschied ist, wie in Argumente übergeben werden, die in diesem Fall keine Rolle spielt.)

+0

Leider, wie sie Rückrufe von 'SomeOtherClass' initiiert sind, tue ich nicht steuern tatsächlich, wie sie heißen. – tdavis

+0

In diesem Fall müssen Sie nur den Rückruf in eine Schließung umbrechen, so dass der Wert von "this" mit der Funktionsreferenz mitgeführt wird. * edsoverflow * hat bereits ein Beispiel gepostet. –

-1

Könnten Sie tun etwas wie das?

var MyPrototype = function() { this.init(); } 
$.extend(MyPrototype.prototype, { 
    a: 5, 
    init: function() { 
     var thing = new SomeOtherClass(); 
     var self = this; 
     thing.meth = function(){this.meth.apply(self)}; 
     // thing.meth() is called somewhere within thing; 
    }, 
    meth: function() { 
     alert(this.a); 
    } 
} 
+0

Ihr "Dies" innerhalb der "Helfer" -Methode, die "Meth" aufruft, hat immer noch das gleiche Problem, das sie ursprünglich hatte. –

+0

Geändert, um es selbst zu verwenden. Funktioniert jetzt gut für mich. – agilefall

1

Da Sie nicht kontrollieren können, wie es Ihnen dies könnte versuchen, genannt:

var MyPrototype = function() { this.init(); } 
$.extend(MyPrototype.prototype, { 
    a: 5, 
    init: function() { 
     var thing = new SomeOtherClass(); 

     // Create an aliad for this 
     var that = this; 
     thing.meth = function() { 
      // You can always access the object using it's "that" alias 
      alert(that.a); 
     }; 
    } 
} 

Oder ...

var MyPrototype = function() { this.init(); } 
$.extend(MyPrototype.prototype, { 
    a: 5, 
    init: function() { 
     var thing = new SomeOtherClass(); 

     // Create an aliad for this 
     var that = this; 
     thing.meth = function() { 
      // You can always access the object using it's "that" alias 
      that.meth(); 
     }; 
    }, 
    meth: { 
     alert(this.a); 
    } 
} 
0

Vor Beginn Ihrer Codebeispiel, fügen Sie diese Zeile:

var self = this; 

ersetzen dann alle Verwendungen von ‚this‘ in Ihrem Code mit ‚Selbst‘.

(ich glaube, ein paar der Antworten auf diese sagen mehr oder weniger das gleiche.)