Javascript Class, var undefined in event listener -


इस सवाल का पहले से ही एक उत्तर है: < / P>

  • 4 उत्तर
  var Foo = (Function () {function Foo ()} {this._s = "string"; this.setButton ();}; Foo.prototype.setButton = function () {document.getElementById ('id')। AddEventListener ('पर क्लिक करें' , फ़ंक्शन (इवेंट) {चेतावनी (यह._एस);});}; फ़ू.प्रोटीटिप.एमएफ़ = फ़ंक्शन () {अलर्ट (यह._एस);}; फ़ुट फू;}) (); Var fo = new Foo (); fo.method ();  

मैं एक बटन को एक बटन से बाँधना चाहता हूं, और एक फ़ंक्शन निष्पादित करना चाहता हूं, जो एक 'निजी' var का उपयोग करता है, जब मैं बटन पर क्लिक करता हूं फ़ंक्शन ठीक से कहा जाता है, लेकिन इसे देख नहीं सकता Var this._s (यह 'अपरिभाषित' लिखता है) यदि मैं fo.method () लिखता हूं तो स्ट्रिंग सही ढंग से मुद्रित है I यहां जेएसफ़िल्ड है:

आपको इसे पारित करने से पहले फ़ंक्शन मैन्युअल का संदर्भ (यह) सेट करना है ।

<पूर्व> Foo.prototype.setButton = function () {var tmpFunc = function (evt) {चेतावनी (this._s); } // स्टोर की सुविधा var boundFunction = tmpFunc.bind (this); // मैन्यूअल कॉन्टेक्ट सेट करें // इवेंटलिस्ट ऑर्डर करने के लिए फ़ंक्शन अभी पास करें। आर्टएलिमेंटबीआईआईडी ('आईडी')। AddEventListener ('क्लिक', बाध्यफंक्शन); };

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

asp.net mvc - How to attach sql database to a javascript graph -

c# - How to know the number of Threads created and limit the Tasks accordingly -