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 8558033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:42:52+00:00 2026-06-11T15:42:52+00:00

I understand that there are many different views on the way JavaScript should be

  • 0

I understand that there are many different views on the way JavaScript should be written, but I was wondering if the way I am writing it is fine. I don’t want to go into the workforce writing code that nobody understands.

The basic premise of this code was to test custom event handlers for objects, was wondering if there is any glaring "YOU SHOULD NOT DO THIS" type thing?

    function EventClass() {
        var self = this;
        var events = {};
        var i = 0;
        self.fire = function(evt, args) {
            for (x in events[evt])
                events[evt][x].call(this, args);
        }

        self.on = function(evt, fn) {
            if (events[evt] == null) {
                events[evt] = []
            }
            events[evt].push(fn);
        }
    };

    function Human(x, y) {
        var self = this;
        self.__proto__ = new EventClass();
        var xCoord = 0;
        var yCoord = 0;

        self.events = {
            "MOVEMENT" : "movement"
        };

        self.init = function(x,y) {
            xCoord = x;
            yCoord = y;
        }

        self.draw = function(context) {
            context.beginPath();
            context.arc(xCoord,yCoord,10,0,Math.PI*2,true);
            context.closePath();
            context.fill();
        }

        self.moveLeft = function() {
            xCoord -= 5;
            self.fire(self.events.MOVEMENT, xCoord);
        }
        self.init(x,y);
    };

    function Player(x, y) {
        var self = this;
        self.__proto__ = new Human();
        self.init(x,y);
    };

    function Canvas(c) {
        var self = this;
        var canvas;
        var context;
        var objects = [];
        
        self.init = function(c) {
            canvas = c;
            context = canvas.getContext("2d");
        };

        this.redraw = function() {
            context.clearRect(0,0,300,300);
            for (x in objects) {
                objects[x].draw(context);
            }
        }

        this.addObject = function(obj) {
            objects.push(obj);
            obj.on(obj.events.MOVEMENT, function(coord) {
                console.log(coord);
                self.redraw();
            });
        };
        self.init(c);
    }


    var canvas = new Canvas(document.getElementById("canvas"));
    var human0 = new Human(75,75);
    canvas.addObject(human0);
    var human1 = new Human(100,100);
    canvas.addObject(human1);
    var player = new Player(200,200);
    canvas.addObject(player);

    canvas.redraw();
  • 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-11T15:42:53+00:00Added an answer on June 11, 2026 at 3:42 pm

    A couple of small things — sort of.

    The first one is tiny.

    Be consistent with the this and self thing.
    The example I’m thinking of here is in your Canvas constructor.

    The truth of the matter is that nothing inside of your particular implementation requires self.
    Unless you’re planning on giving your methods to other objects, or callbacks, self isn’t required, there, until you get into writing functions which exist INSIDE of the methods.
    And even then, all self will help with is accessing the public properties/methods of that particular instance, and wouldn’t help you access events or i or whatever.

    Not to say that it’s not good to do — it’s just more-useful when you’re doing the object-composition thing, rather than object-construction thing (and frequently becomes downright necessary, then).

    The second thing was more of a problem.
    __proto__ might be reasonably-well supported, if you’re talking about FireFox/Chrome, but if you need to support wide swaths of browsers, you’re going to make your life much more difficult by doing things that way, rather than adding to the ConstructorFN.prototype object.

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

Sidebar

Related Questions

There are things that I don't understand when it comes to linking... I'm writing
Is there a way to debug CoffeeScript line-by-line? I understand that it compiles into
There is something that I really don't understand with the HttpListener. The code below
I'm beginning and there is something that I don't understand with pointers. I have
I know that on stackoverflow there are many similar questions but mine is a
I understand that there is a setting in Sun JVM for dumping the heap
Are there overall rules/guidelines for what makes a method thread-safe? I understand that there
I am trying to understand the co-ordinate spaces of three.js, I understand that there
Just to preface my question, I understand that there is no direct support for
Why only one overload throws this exception? Little update: I understand that there was

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.