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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:35:08+00:00 2026-05-27T18:35:08+00:00

Just experimenting with JS + canvas and i seem to have hit a wall.

  • 0

Just experimenting with JS + canvas and i seem to have hit a wall.
the “aim” of my minimal application is to click at any random place on the canvas, press the draw button and squares draw where you clicked.

coming from an OO background… i (tried) to use OO, which in js i dont fully have the grasp of.

but basically i have a custom Square object

function Square(l, w, x, y) {

    this.length = l;
    this.width  = w;
    this.posx   = x - l/2;
    this.posy   = y - w/2;

    //test
    //ctx.fillStyle = "rgb(20,0,0)";
    //ctx.fillRect(this.posx,this.posy,this.length,this.width);


    this.draw = function() {

        ctx.fillStyle = "rgb(20,0,0)";
        ctx.fillRect(this.posx,this.posy,this.length,this.width);

    }
}

which i add to an array every time the user clicks…
here is the event handler for when i click on the canvas.

function addTo(evt) {

    pos = getMousePos(evt);
    var sq = new Square(50, 50, pos.x, pos.y);
    list.push(sq);

    output.innerText = "("+sq.posx+","+sq.posy+")";
}

and here is where i (attempt) to draw the squares.

function renderStack() {

    //alert(list);
    canvas.width = canvas.width;
    for(var o in list) o.draw();

}

and this is the error:

Uncaught TypeError: Object 0 has no method 'draw'

i get a similar error trying to access the variables for that object.
It seems that after i add them to the list js forgets about what type they are? – because when i print the array it is full of [Object object]’s

thanks.

  • 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-27T18:35:09+00:00Added an answer on May 27, 2026 at 6:35 pm
    for(var o in list)
    

    o is the index (or property) of array. You will access what’s stored at that index by list[o].

    But for the arrays, it is better to use for loop as shown by Alnitak than for-in.

    Update:

    For-in is to be used when you need to loop through all the properties. Since array is an object it has indices and properties. So for-in will iterate through all the indices and properties. for-in is better for objects.

    e.g.

    var obj = { p1: 1, p2: 2};
    
    for(var prop in obj) {
       //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just begun experimenting with the DotNetOpenAuth project. Modifying the sample OpenIdRelyingPartyMvc project,
I've only just started looking at Dapper.net and have just been experimenting with some
I'm using a PowerPC 750 and have just started experimenting with the external interrupt.
I am just experimenting something with jQuery. I have an image which fades into
I'm a MonoTouch 4 newbie and just experimenting with random colors on the window
I have an android app I am just experimenting things on and I cannot
I just started experimenting with java today (have experience with javascript and PHP) and
I'm just experimenting with Optimistic locking. I have the following class: @Entity public class
I'm just experimenting with boost::pool to see if its a faster allocator for stuff
I just started experimenting with Aptana Jaxer server side javascript engine for my next

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.