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

  • SEARCH
  • Home
  • 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 7665043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:27:36+00:00 2026-05-31T14:27:36+00:00

I’m trying to do more OOP in Javascript. One thing i can’t figure out

  • 0

I’m trying to do more OOP in Javascript.
One thing i can’t figure out is how can i modify a variable inside an object from another function?

Here’s how I tried to do it:

function Ball(){
    radius = 5;
    Y = 20;
    X = 25; // The value i would like to change.
    ctx.arc(this.X, this.Y, this.radius, 0, Math.PI*2, true);
    ctx.fillStyle = '#00ff00';
    ctx.fill();
}

function draw(){
    Player();
    Ball();
}

function update(){
    ctx.clearRect(0, 0, 800, 400);
    draw();
    Ball.X++;  // This is where i want to modify the value.

}

So far I’ve only been able to do it if I define X as a global variable, but I dont want to do that since there are other X and Y values.

So how would i access the variable from outside the function?

EDIT:
The solution provided by “nnnnnn” worked to an extent, it changes the X value but I ran into another problem.
My clearRect doesnt clear the animation, so instead of a ball it draws a line that just grows.

This is what the code looks like right now:

function Ball(){
    this.radius = 5;
    this.Y = 20;
    this.X = 25;
    this.draw = function() {
       ctx.arc(this.X, this.Y, this.radius, 0, Math.PI*2, true);
       ctx.fillStyle = '#00ff00';
       ctx.fill();
    };
}

var ball = new Ball();

function draw(){
    Player();
    ball.draw();
}


function update(){
    ctx.clearRect(0, 0, 800, 400);
    draw();
    ball.X++;
}

I have tried moving it around, placing it both in the draw() and ball.draw() functions, but still getting the same result, I also tried doing a fillRect instead of clear but it didnt help.
Anyone able to see whats wrong?

  • 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-05-31T14:27:37+00:00Added an answer on May 31, 2026 at 2:27 pm

    “So far I’ve only been able to do it if I define X as a global variable”

    Actually your variables radius, X and Y variables are all global variables. For them to be properties of an object they need to be set as follows:

    someObject.radius = 5;
    // OR
    someObject["radius"] = 5;
    

    They’re not local variables within the Ball() function because they’re not declared with var – any variables that you assign a value to without declaring them with var automatically become global.

    Your Ball() function is not creating an object when you call it – in order for it to do so you need to use new:

    var ball = new Ball();
    

    If you call it with new then JS automatically creates a new instance of Ball and within the function this refers to that new instance. So the function should say:

    function Ball(){
        this.radius = 5;
        this.Y = 20;
        this.X = 25; // The value i would like to change.
        ctx.arc(this.X, this.Y, this.radius, 0, Math.PI*2, true);
        ctx.fillStyle = '#00ff00';
        ctx.fill();
    }
    

    Then you can access and change properties as follows:

    var ball = new Ball();
    alert(ball.X); // 25
    ball.X++;
    

    However that doesn’t really fit with the way you’ve structured your code because you’re trying to call Ball() to make it draw itself. You probably want something more like this:

    function Ball(){
        this.radius = 5;
        this.Y = 20;
        this.X = 25;
        this.draw = function() {
           ctx.arc(this.X, this.Y, this.radius, 0, Math.PI*2, true);
           ctx.fillStyle = '#00ff00';
           ctx.fill();
        };
    }
    
    var ball = new Ball();
    
    function draw(){
        Player();
        ball.draw();
    }
    
    function update(){
        ctx.clearRect(0, 0, 800, 400);
        draw();
        ball.X++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

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.