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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:13:21+00:00 2026-05-27T13:13:21+00:00

Hi I am quite new to javascript. So I just tried to create a

  • 0

Hi I am quite new to javascript. So I just tried to create a simple object that draws two squares into the canvas. To understand object orientation a bit better I put them in different objects (where drawtest.js is the main). Unfortunately I dont see where my error lies. I have been searching posts on stackoverflow for hours, even the example from my prof looks (similiar) like mine, I don’t see the difference but his version works. In firebug I get an:

testobject.draw is not a function

What did I forgot or made wrong?

“index.html”

<!DOCTYPE html>
<html>  

<head>  
<title>simple object test</title>
<script src="js/drawtest.js" type="text/javascript"></script>
<script src="js/testobject.js" type="text/javascript"></script>

</head>  

<body>

<canvas id="canvas" width="300" height="300"></canvas>  

</body>  

</html> 

the testfile “drawtest.js”

window.onload = function(){ init(); };

var canvas;
var context;

function init() 
{
canvas = document.getElementById("canvas");  
context = canvas.getContext("2d"); 

testobject = new Test(context);

testobject.draw();  //this is where I get testobject.draw is not a function

}

the object “testobject.js”

function Test(context){

this.context = context;


Test.prototype=
{

draw:.
function()
{   
this.context.fillStyle = "rgb(200,0,0)";  
this.context.fillRect (10, 10, 55, 50);  

this.context.fillStyle = "rgba(0, 0, 200, 0.5)";  
this.context.fillRect (30, 30, 55, 50);  
}
};
}
  • 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-27T13:13:21+00:00Added an answer on May 27, 2026 at 1:13 pm

    A few problems:

    1. You have a syntax error:

      draw:.
      //   ^--- here
      

      Remove the dot. The syntax error should have shown up in the JavaScript/error console of whatever browser you’re using. Kick around the menu system of your browser to find its developer tools; you’ll want to get familiar with them. Chrome has quite a good set, recent versions of Firefox have them built in, older versions can use the Firebug plug-in.

    2. It’s best not to replace a constructor function’s prototype; instead, just add to it:

      Test.prototype.draw = function() {   
          this.context.fillStyle = "rgb(200,0,0)";  
          this.context.fillRect (10, 10, 55, 50);  
      
          this.context.fillStyle = "rgba(0, 0, 200, 0.5)";  
          this.context.fillRect (30, 30, 55, 50);  
      };
      
    3. You don’t want to make your prototype modifications within the constructor function, move that part outside the constructor function. So the Test function and its associated prototype properties becomes:

      function Test(context){
      
          this.context = context;
      }
      
      Test.prototype.draw = function() {   
          this.context.fillStyle = "rgb(200,0,0)";  
          this.context.fillRect (10, 10, 55, 50);  
      
          this.context.fillStyle = "rgba(0, 0, 200, 0.5)";  
          this.context.fillRect (30, 30, 55, 50);  
      };
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im quite new at Javascript however I have been trying to create a currency
I'm quite new to Javascript and I was just reading following article. you can
I am quite new to JavaScript and just started some more serious development in
I'm quite new to javascript but have undertaken a task to get better aquainted
Dear experts, I am quite new to javascript and I often see coders use
Yo. I'm really quite new to this whole JavaScript business, not to mention AJAX,
Iam quite new to functions in SQL and I would like to create a
i'm quite new to Dojo, but i am trying to convert an existing javascript
I am quite new to javascript, but not to programing in general. What I
i tried to create a simple css background-image transition usind the jquery fadeIn fadeOut

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.