www.webdeveloper.com
Results 1 to 2 of 2

Thread: Javascript Prototype (Very Confusing)

  1. #1
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    76

    Javascript Prototype (Very Confusing)

    Here's my code:
    Code:
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    
    
    var Shape = function(){}
    
    Shape.prototype.Circle = function(){
    	// Some code here
    };
    
    Shape.prototype.Square = function(){
    	// Some code here
    };
    
    
    MainMenu.prototype = new Shape();
    
    var MainMenu = function(ctx){
       this.draw{
    	this.Circle();
    	this.Square();
       }
    }
    I know you can do this:
    Code:
    MainMenu.Circle();
    MainMenu.Square();
    What I want to do is to call the Circle() and Square() function inside my draw() function.
    Is this even possible??
    I'm going crazy trying to figure this out!!

  2. #2
    Join Date
    Mar 2009
    Posts
    485
    As you know, what you have done doesn't and isn't going to work. If you are going to use the new operator to bring an object into existence you need a constructor function. You might think you have one but you don't.

    Instead of:
    Code:
    var Shape = function(){}
    you need:

    Code:
    function Shape(){};
    This creates a Constructor.

    You don't use [whatever].prototype on INSTANCES of objects, you use it on the Constructor.

    And as for the rest of your code: experiment and try to find out what's wrong with that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles