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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:50:05+00:00 2026-06-09T10:50:05+00:00

I am confused with what prototype actually does. I am learning HTML canvas right

  • 0

I am confused with what prototype actually does. I am learning HTML canvas right now and for one of the examples, it uses prototype to declare the draw method. But whats the difference between using prototype versus simply putting it in the constructor function itself?

Isn’t this example from the book:

function Ball (radius, color) {
    if (radius === undefined) { radius = 40; }
    if (color === undefined) { color = "#ff0000"; }
    this.x = 0;
    this.y = 0;
    this.radius = radius;
    this.rotation = 0;
    this.scaleX = 1;
    this.scaleY = 1;
    this.color = utils.parseColor(color);
    this.lineWidth = 1;
    }


Ball.prototype.draw = function (context) {
context.save();
context.translate(this.x, this.y);
context.rotate(this.rotation);
context.scale(this.scaleX, this.scaleY);
context.lineWidth = this.lineWidth;
context.fillStyle = this.color;
context.beginPath();
//x, y, radius, start_angle, end_angle, anti-clockwise
context.arc(0, 0, this.radius, 0, (Math.PI * 2), true);
context.closePath();
context.fill();
if (this.lineWidth > 0) {
context.stroke();
}
context.restore();
};

The same as putting this in?:

function Ball(radius, color){
...
this.draw = function (context) {
    context.save();
    context.translate(this.x, this.y);
    context.rotate(this.rotation);
    context.scale(this.scaleX, this.scaleY);
    context.lineWidth = this.lineWidth;
    context.fillStyle = this.color;
    context.beginPath();
    //x, y, radius, start_angle, end_angle, anti-clockwise
    context.arc(0, 0, this.radius, 0, (Math.PI * 2), true);
    context.closePath();
    context.fill();
    if (this.lineWidth > 0) {
    context.stroke();
    }
    context.restore();
    };
}
  • 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-09T10:50:15+00:00Added an answer on June 9, 2026 at 10:50 am

    prototype is an Object shared by all other objects who take it as prototype, which results in that methods added to prototype dynamically can be shared by all instances.

    function ClassA(){
        this.sayHello = function(){
            return "hello!";
        }
    }
    
    var instanceA = new ClassA();
    instanceA.sayHello();//return "hello!";
    //add a method to instanceA
    instanceA.sayBye = function(){ return "Bye!"; }
    
    var instanceB = new ClassA();
    instanceB.sayBye(); //error, sayBye is not a method of instanceB.
    
    //But, this really works
    ClassA.prototype.sayBye = function(){ return "Bye!"; }
    

    And, since all instance share the single prototype, all methods only stay in one place in memory. In your second implementation, each instance has its own method, which resulting in a lot of memory using.

    Keeping method out of the definition of Class makes code more clean and readable, although this is not a strong evidence .

    With prototype, it’s more easy for developers to write code in OOP style.

     function ClassB(){
     }
     ClassB.prototype = new ClassA();
     // The equivalent approach may be this
     function ClassB(){
         ClassA.apply(this);
     }
    

    Both of the two approaches can do the same jobs, so choose any one you like.

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

Sidebar

Related Questions

I'm confused where should I use prototype to declare a method ? What I
Pretty confused right now, while i feel i know a bit about vhosts this
This is a continuation of Confused as to which Prototype helper to use .
confused - given a track uri, I can get one album but possibly more
I'm working with the source code for VirtualJoystick and I'm confused about the method
I was looking into the prototype object, and i am a bit confused about
Learning C here, and I'm pretty confused on how to use function prototypes. I'm
Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I am confused with these
I'm just now getting into instantiating JavaScript object literals using a prototype constructor, but
I'm learning about Win32 programming, and the WinMain prototype looks like: int WINAPI WinMain

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.