使用jQuery创建一个简单的JavaScript类

前端之家收集整理的这篇文章主要介绍了使用jQuery创建一个简单的JavaScript类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试理解jQuery类,但它不是很顺利.

我的目标是以这种方式使用类(或者学习更好的方法):

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

使用其他人的例子,这就是我尝试过的:

  1. $.Player = function ($) {
  2.  
  3. };
  4.  
  5. $.Player.prototype.InitEvents = function () {
  6.  
  7. $(this).keypress(function (e) {
  8. var key = e.which;
  9. if (key == 100) {
  10. MoveRight();
  11. }
  12. if (key == 97) {
  13. MoveLeft();
  14. }
  15. });
  16. };
  17.  
  18. $.Player.prototype.MoveRight = function () {
  19. $(this).css("right",this.playerX += 10);
  20. }
  21.  
  22. $.Player.prototype.MoveLeft = function () {
  23. $(this).css("right",this.playerX -= 10);
  24. }
  25.  
  26. $.Player.defaultOptions = {
  27. playerX: 0,playerY: 0
  28. };

最终目标是使用键盘字母A和D左右移动屏幕上的角色.

我有一种感觉,我在这个“班级”做错了
但我不确定为什么.

(对不起我的英语不好)

解决方法

一个重要的问题是你必须将传递的jQuery对象/元素分配给this.element – 或另一个this.propertyName – 以便稍后可以在实例的方法中访问它.

你也不能直接调用MoveRight()/ MoveLeft(),因为这些函数没有在作用域链中定义,而是在实例的构造函数的原型中定义,因此你需要引用实例本身来调用它们.

更新和评论代码如下:

  1. (function ($) { //an IIFE so safely alias jQuery to $
  2. $.Player = function (element) { //renamed arg for readability
  3.  
  4. //stores the passed element as a property of the created instance.
  5. //This way we can access it later
  6. this.element = (element instanceof $) ? element : $(element);
  7. //instanceof is an extremely simple method to handle passed jQuery objects,//DOM elements and selector strings.
  8. //This one doesn't check if the passed element is valid
  9. //nor if a passed selector string matches any elements.
  10. };
  11.  
  12. //assigning an object literal to the prototype is a shorter Syntax
  13. //than assigning one property at a time
  14. $.Player.prototype = {
  15. InitEvents: function () {
  16. //`this` references the instance object inside of an instace's method,//however `this` is set to reference a DOM element inside jQuery event
  17. //handler functions' scope. So we take advantage of JS's lexical scope
  18. //and assign the `this` reference to another variable that we can access
  19. //inside the jQuery handlers
  20. var that = this;
  21. //I'm using `document` instead of `this` so it will catch arrow keys
  22. //on the whole document and not just when the element is focused.
  23. //Also,Firefox doesn't fire the keypress event for non-printable
  24. //characters so we use a keydown handler
  25. $(document).keydown(function (e) {
  26. var key = e.which;
  27. if (key == 39) {
  28. that.moveRight();
  29. } else if (key == 37) {
  30. that.moveLeft();
  31. }
  32. });
  33.  
  34. this.element.css({
  35. //either absolute or relative position is necessary
  36. //for the `left` property to have effect
  37. position: 'absolute',left: $.Player.defaultOptions.playerX
  38. });
  39. },//renamed your method to start with lowercase,convention is to use
  40. //Capitalized names for instanceables only
  41. moveRight: function () {
  42. this.element.css("left",'+=' + 10);
  43. },moveLeft: function () {
  44. this.element.css("left",'-=' + 10);
  45. }
  46. };
  47.  
  48.  
  49. $.Player.defaultOptions = {
  50. playerX: 0,playerY: 0
  51. };
  52.  
  53. }(jQuery));
  54.  
  55. //so you can use it as:
  56. var player = new $.Player($("#playerElement"));
  57. player.InitEvents();

Fiddle

另请注意,JavaScript没有实际的“类”(至少在ES6实现之前),也没有方法(根据定义只与Classes关联),而是提供类似于类的甜蜜语法的构造函数.这是一篇由TJ Crowder撰写的关于JS“假”方法的精彩文章,它有点先进,但每个人都应该能够从阅读中学到新东西:
http://blog.niftysnippets.org/2008/03/mythical-methods.html

猜你在找的jQuery相关文章