jQuery

2013-01-19 8 views
8

के साथ एक साधारण जावास्क्रिप्ट क्लास बनाना I jQuery वर्गों को समझने की कोशिश कर रहा हूं लेकिन यह बहुत अच्छा नहीं चल रहा है।jQuery

मेरा लक्ष्य एक वर्ग इस तरह से उपयोग करने के लिए (या यह करने के लिए एक बेहतर तरीका जानने के लिए) है:

var player = new Player($("playerElement")); 
player.InitEvents(); 

अन्य लोगों के उदाहरण का उपयोग करना, यह मैं क्या करने की कोशिश की है:

$.Player = function ($) { 

}; 

$.Player.prototype.InitEvents = function() { 

    $(this).keypress(function (e) { 
     var key = e.which; 
     if (key == 100) { 
      MoveRight(); 
     } 
     if (key == 97) { 
      MoveLeft(); 
     } 
    }); 
}; 

$.Player.prototype.MoveRight = function() { 
    $(this).css("right", this.playerX += 10); 
} 

$.Player.prototype.MoveLeft = function() { 
    $(this).css("right", this.playerX -= 10); 
} 

$.Player.defaultOptions = { 
    playerX: 0, 
    playerY: 0 
}; 

अंत लक्ष्य कुंजीपटल अक्षरों A और D का उपयोग कर स्क्रीन पर बाईं ओर दाईं ओर एक चरित्र चलाना है।

मुझे एहसास है कि मैं इस "कक्षा" के साथ कुछ गलत कर रहा हूं लेकिन मुझे यकीन नहीं है कि क्यों।

(मेरी अंग्रेजी के लिए खेद है)

+0

'उदाहरण तरीकों अंदर this' का संदर्भ उदाहरण वस्तु स्वयं ही आप '$ (यह) .keypress',' $ (this) .css' आदि का उपयोग नहीं कर सकते हैं क्योंकि यह 'D' तत्व और न ही क्वेरी स्ट्रिंग का संदर्भ नहीं देता है। आपकी फ़ंक्शन कॉल भी गलत हैं, इसे 'इसे पढ़ना चाहिए।MoveRight() 'लेकिन जैसा कि आप एक jQuery हैंडलर के अंदर हैं जो 'इस' संदर्भ को डीओएम तत्व में ही सेट करता है, आपको आवृत्ति के 'यह' को चरम श्रृंखला में एक चर के स्तर को असाइन करने की आवश्यकता होगी ताकि आप कर सकें इसे 'MoveRight'/'MoveLeft' विधियों को कॉल करने के लिए हैंडलर के अंदर पहुंचें। –

+0

क्या आप मुझे दिखा सकते हैं कि मैंने अपनी समस्या को ठीक करने के लिए अपना कोड कैसे बदल दिया? यह देखने के लिए मेरे लिए बहुत आसान होगा कि मैंने क्या किया है अगर मैं एक अच्छा कोड बनाम मेरा सामना कर सकता हूं। (यदि बहुत कठिन नहीं है) – samy

+0

मैं आदमी को आजमा सकता हूं, लेकिन यह "फिक्स" की तुलना में एक पूर्ण पुनर्लेखन के करीब है। ': पी' मैं देखूंगा कि क्या मैं एक साधारण उदाहरण बना सकता हूं। –

उत्तर

19

एक महत्वपूर्ण मुद्दा आप एक this.element के लिए पारित किया jQuery वस्तु/तत्व आवंटित करने के लिए है कि है - या किसी अन्य this.propertyName - तो आप उदाहरण के तरीकों के अंदर बाद में इसका उपयोग कर सकते हैं।

तुम भी MoveRight()/MoveLeft() सीधे उस तरह फोन नहीं कर सकते क्योंकि उन कार्यों गुंजाइश श्रृंखला में परिभाषित नहीं कर रहे हैं, बल्कि अपने उदाहरण के निर्माता के प्रोटोटाइप में है, इसलिए आप उदाहरण खुद के लिए एक संदर्भ की जरूरत है इन कॉल करने के लिए।

अपडेट किया गया और नीचे दिए गए कोड टिप्पणी की: जो परिभाषा से जुड़े रहे हैं

(function ($) { //an IIFE so safely alias jQuery to $ 
    $.Player = function (element) { //renamed arg for readability 

     //stores the passed element as a property of the created instance. 
     //This way we can access it later 
     this.element = (element instanceof $) ? element : $(element); 
     //instanceof is an extremely simple method to handle passed jQuery objects, 
     //DOM elements and selector strings. 
     //This one doesn't check if the passed element is valid 
     //nor if a passed selector string matches any elements. 
    }; 

    //assigning an object literal to the prototype is a shorter syntax 
    //than assigning one property at a time 
    $.Player.prototype = { 
     InitEvents: function() { 
      //`this` references the instance object inside of an instace's method, 
      //however `this` is set to reference a DOM element inside jQuery event 
      //handler functions' scope. So we take advantage of JS's lexical scope 
      //and assign the `this` reference to another variable that we can access 
      //inside the jQuery handlers 
      var that = this; 
      //I'm using `document` instead of `this` so it will catch arrow keys 
      //on the whole document and not just when the element is focused. 
      //Also, Firefox doesn't fire the keypress event for non-printable 
      //characters so we use a keydown handler 
      $(document).keydown(function (e) { 
       var key = e.which; 
       if (key == 39) { 
        that.moveRight(); 
       } else if (key == 37) { 
        that.moveLeft(); 
       } 
      }); 

      this.element.css({ 
       //either absolute or relative position is necessary 
       //for the `left` property to have effect 
       position: 'absolute', 
       left: $.Player.defaultOptions.playerX 
      }); 
     }, 
     //renamed your method to start with lowercase, convention is to use 
     //Capitalized names for instanceables only 
     moveRight: function() { 
      this.element.css("left", '+=' + 10); 
     }, 
     moveLeft: function() { 
      this.element.css("left", '-=' + 10); 
     } 
    }; 


    $.Player.defaultOptions = { 
     playerX: 0, 
     playerY: 0 
    }; 

}(jQuery)); 

//so you can use it as: 
var player = new $.Player($("#playerElement")); 
player.InitEvents(); 

Fiddle

भी ध्यान रखें कि जावास्क्रिप्ट वास्तविक "वर्ग" नहीं है (कम से कम जब तक नहीं ES6 लागू किया जाता है) और न ही तरीके (विशेष रूप से कक्षाओं के लिए), बल्कि रचनाकार जो कक्षाओं जैसा दिखता है एक मीठा वाक्यविन्यास प्रदान करते हैं। यहाँ एक भयानक टीजे Crowder द्वारा लिखित लेख जे एस के "नकली" तरीकों के बारे में है, यह एक छोटे से उन्नत है, लेकिन हर किसी को इसे पढ़ने से कुछ नया सीखने के लिए सक्षम होना चाहिए:
http://blog.niftysnippets.org/2008/03/mythical-methods.html

2

आप this अपने Player प्रोटोटाइप काम करता है, this अंक वर्तमान प्लेयर वस्तु को अंदर का उपयोग करते हैं।

लेकिन जब आप $(this).keypress का उपयोग करते हैं तो इसे this अंक HTML तत्व के लिए आवश्यक है।

दोनों बस असंगत हैं। केवल एक this है और यह वर्तमान प्लेयर ऑब्जेक्ट को इंगित करता है, न कि HTML तत्व के लिए।

अपनी समस्या को ठीक करने के लिए, आपको HTML ऑब्जेक्ट को प्लेयर ऑब्जेक्ट में अपनी सृजन या प्रासंगिक फ़ंक्शन कॉल में पास करने की आवश्यकता होगी।

आप प्लेयर इस तरह निर्माण पर वस्तु में तत्व पारित कर सकते हैं:

$.Player = function ($, element) { 
     this.element = element; 

}; 

$.Player.prototype.InitEvents = function() { 

    $(this.element).keypress(function (e) { 
     var key = e.which; 
     if (key == 100) { 
      MoveRight(); 
     } 
     if (key == 97) { 
      MoveLeft(); 
     } 
    }); 
}; 

$.Player.prototype.MoveRight = function() { 
    $(this.element).css("right", this.playerX += 10); 
} 

$.Player.prototype.MoveLeft = function() { 
    $(this.element).css("right", this.playerX -= 10); 
} 

$.Player.defaultOptions = { 
    playerX: 0, 
    playerY: 0 
}; 
+0

मेरी अज्ञानता के लिए खेद है लेकिन मैं इस कक्षा का उपयोग कैसे करूं? मेरा मतलब है कि अगर मैं इसका इस्तेमाल करता हूं तो मैं अपने qustion में क्यों दिखाता हूं यह काम नहीं करता है। (यदि इतना कठिन नहीं है) क्या आप एक उदाहरण जोड़ सकते हैं? (मेरे प्रश्न का उत्तर देने के लिए समय निकालने के लिए धन्यवाद) – samy