Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:31:48+00:00 2026-06-17T13:31:48+00:00

I’m trying to understand jQuery classes but it is not going very well. My

  • 0

I’m trying to understand jQuery classes but it is not going very well.

My goal is to use a class this way (or to learn a better way to do it):

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

Using other people’s examples, this is what I tried:

$.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
};

The end goal is to have a character moving on the screen left and right using the keyboard letters A and D.

I have a feeling that I’m doing something very wrong with this “class”
but I’m not sure why.

(sorry for my English)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:31:50+00:00Added an answer on June 17, 2026 at 1:31 pm

    An important issue is that you have to assign the passed jQuery object/element to a this.element – or another this.propertyName – so you can access it later inside the instance’s methods.

    You also cannot call MoveRight()/MoveLeft() directly like that because those functions are not defined up in the scope chain, but rather in the prototype of your instance’s Constructor, hence you need a reference to the instance itself to call these.

    Updated and commented code below:

    (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

    Also note that JavaScript does not have actual “classes” (at least not until ES6 gets implemented) nor Methods (which by definition are associated exclusively to Classes), but rather Constructors which provide a sweet syntax that resembles classes. Here’s an awesome article written by TJ Crowder regarding JS’s “fake” methods, it is a little advanced but everyone should be able to learn something new from reading it:
    http://blog.niftysnippets.org/2008/03/mythical-methods.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.