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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:09:18+00:00 2026-06-14T21:09:18+00:00

How do I extend an object type for ONLY a specific object type? I

  • 0

How do I extend an object type for ONLY a specific object type? I don’t want conflicts with another library using object.color().
At the moment, I’m using:

function random_int(start,end) {
    return Math.floor(Math.random()*(end-start+1)+start);
}
var balls = function balls (context) {
    this.canvas = context.canvas;
    this.context = context;
}
Object.prototype.ball = function ball() { }
balls.prototype.add = function () {
  this.context.fillStyle = '#ABC123';
  this.radius = 15;
  this.x = random_int(this.radius+1,this.canvas.width-this.radius-1);
  this.y = random_int(this.radius+1,this.canvas.height-this.radius-1);
  return this;
}
Object.prototype.draw = function () {
  this.context.beginPath();
  this.context.arc(this.x, this.y, this.radius, 0, (Math.PI/180)*360, false);
  this.context.fill();
  this.context.closePath();
}
balls.prototype.redraw = function () {
    this.context.save();
    this.context.setTransform(1, 0, 0, 1, 0, 0);
    this.context.clearRect(0, 0, canvas.width, canvas.height);
    this.context.restore();
    this.draw();
}
Object.prototype.color = function (col) {
    this.context.fillStyle = col;
}
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.canvas.width  = window.innerWidth*0.9;
context.canvas.height = window.innerHeight*0.9;
var Balls = new balls(context);
for (var i = 0; i<10;i++) {
    var b = Balls.add();
    b.color("#"+((1<<24)*Math.random()|0).toString(16));
    b.draw();
}

jsfiddle: http://jsfiddle.net/hNy3r/1/

Using two classes as I plan to loop through each of the balls in the Balls class later.

  • 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-14T21:09:20+00:00Added an answer on June 14, 2026 at 9:09 pm

    Ending up doing this:

    function random_int(start, end) {
        return Math.floor(Math.random() * (end - start + 1) + start);
    }
    var balls = function balls(context) {
        this.context = context;
        this.add = function () {
            return new ball(context);
        }
    }
    Object.prototype.ball = function ball(context) {
                    this.context = context;
                    this.canvas = context.canvas;
        this.context.fillStyle = '#ABC123';
        this.radius = 15;
        this.x = random_int(this.radius + 1, this.canvas.width - this.radius - 1);
        this.y = random_int(this.radius + 1, this.canvas.height - this.radius - 1);
        this.draw = function () {
            this.context.beginPath();
            this.context.arc(this.x, this.y, this.radius, 0, (Math.PI / 180) * 360, false);
            this.context.fill();
            this.context.closePath();
        }
        this.redraw = function () {
            this.context.save();
            this.context.setTransform(1, 0, 0, 1, 0, 0);
            this.context.clearRect(0, 0, canvas.width, canvas.height);
            this.context.restore();
            this.draw();
        }
        this.color = function (col) {
            this.context.fillStyle = col;
        }
        return this;
    }
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    context.canvas.width = window.innerWidth * 0.9;
    context.canvas.height = window.innerHeight * 0.9;
    var Balls = new balls(context);
    for (var i = 0; i < 10; i++) {
        var b = Balls.add();
        b.color("#" + ((1 << 24) * Math.random() | 0).toString(16));
        b.draw();
    }
    

    Called new ball() from inside balls

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

Sidebar

Related Questions

It's very easy to copy a javascript object in jQuery using jQuery.extend() function call.
I want to be able to have an object extend Enumerable in Ruby to
My code: myobj = new Object(); classes.testegy = Class.extend({ init: function (token) { console.log(test
I am trying to extend Set::Object in order to allow type constraint checking when
I'm trying to create a container that would accept specific classes only. Using generics
If dynamic resolves to object at compile time, and all .NET types extend object,
I tried to extend Socket object something as... public class TestSocket extends Socket {
Whenever I try to extend the Object prototype, I get an error: Error #1056:
Sample Code: var Day = Ember.Object.extend({ date:null, activities:null, // is set to an Em.ArrayProxy
I tried to use object expression to extend the IDelegateEvent, but in fsi there

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.