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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:12:00+00:00 2026-06-16T15:12:00+00:00

Hey so I started to pick up Javascript and I’m having a little bit

  • 0

Hey so I started to pick up Javascript and I’m having a little bit of trouble with objects.
I’m trying to create a shape class that takes in a number of sides. Using these sides, it creates more characteristics so it can store the co-ordinates of the location of a point.
What I have right now is a class that is taking in the size, and I want to use a for loop to create the “properties” in which to store the positions. Just for learning purposes I’m setting them to 0, to see if it’s even possible to do this. Any clarification on objects would be appreciated.

function Shape(size) {
    this.size = size
    for(var i=0; i<size; i++){ //tries to create the properties
        //this[i].posX: 0;
        //this[i].posY = 0;
    }
}

Ideally, I want to access them so it’s in this type of format:

var triangle = new Shape(3);
triangle[0].posX = 100; // So essentially I could set this to 100, the integer in the [] would represent a side.
triangle[0].posY = 100; // etc ... for the rest of the sides

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-06-16T15:12:02+00:00Added an answer on June 16, 2026 at 3:12 pm

    Since the shape can have a variable number of sides, I would recommend creating an Array of points as a property of the Shape class.

    function Shape(size) {
        this.size = size;
        this.point = new Array();//stores an Array of Points
        for(var i=0; i<size; i++){
            this.point[i] = new Point(0, 0);
        }
    }
    
    function Point(x, y){
        this.posX = x || 0;
        this.posY = y || 0;
    };
    

    This way you can create a triangle with the following code:

    // Creates a triangle with the points at (100, 100), (0, 0), and (0, 0)
    var triangle = new Shape(3);
    triangle.point[0].posX = 100;
    triangle.point[0].posY = 100;
    

    I hope this helps.

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

Sidebar

Related Questions

Hey, I've just started learning JavaScript and I'm making a little script that generates
Hey folks. Trying to get started with iPhone video capture stuff, and having a
Hey... I started using Javascript MVC SproutCore Framework (sproutcore.com) and I found it impressive.
hey guys I just started trying to convert my query structure to PDO and
Hey! I've finally started feeling comfortable using jQuery, so now I'm trying to improve
Hey all. I just started looking into the cocos2d library. I've heard that it's
Hey guys today I started to develop my own little alarmclock app. But now
Hey all, I'm trying to fix up my numerous scenarios that test out pages
Hey, I just started wondering about this as I came upon a code that
Hey guys I am trying to write a little text game in my free

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.