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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:16:19+00:00 2026-05-23T07:16:19+00:00

Okay guys, I’m sure this has a very simple solution but my searches are

  • 0

Okay guys, I’m sure this has a very simple solution but my searches are turning up nothing and I’m certain I am misunderstanding something very simple.

All I want to do is create an object that contains 2 methods, say for example initialise() and zoom(), at the end of initialise I want to call the zoom method, initialize sets instance variables like so ‘this.width = 100’, the problem is I cant call zoom() from intialise() if I declare zoom like so

this.zoom = function() {...};

if I declare it like this:

function zoom() {...};

I can call it from initialize but when I try and use the instance variables they come back as undefined, I also tried defining the method as a prototype but that has the same problem as the first method, when I call them from outside the object everything works as expected, however when I try to call the method from another method firebug just tells me the method does not exist.

As I said Im sure there is a simple explanation to do, please see the source code listing below for more details, any help greatly appreciated, thanks.

I’m sure more experienced people will see a whole host of problems in this code, it is very much a learning exercise to try and learn oop in javascript (which is extremely odd if you don’t mind me saying) but if possible please lets just deal with the problem at hand and not get sidetracked on other issues, no doubt I shall return with them in another thread.

function maplocation(x, y, width, height, text, id, filename, from, to, folder)
{
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.text = text;
    this.id = id;
    this.canvasID = id + "canvas";
    this.containerID = id + "container";
    this.ctx;
    this.isZoomed = false;
    this.filename = filename;

    this.addLocation = function(element)
    {
        if(supports_canvas())
        {
            // insert location html
            var returnHTML = 
            '<div id="'+this.containerID+'" style="z-index:5; position:absolute; top:' + this.y + 'px; left:' + this.x + 'px">'+
                '<canvas id="' + this.canvasID + '" width="' + this.width +'" height="' + this.height + '" style="position:absolute; top:0px; left:0px">' +
                '</canvas>' +
                '<div id="' + this.id + '" style="text-align:center; padding:15px; position:absolute; top:0px; left:0px; width:' + (this.width - 30) + 'px; height:' + this.height + 'px; cursor:pointer">' +
                    text +  
                '</div>' +
            '</div>';

            $("#"+element).append(returnHTML);

            // draw into canvas
            this.ctx = document.getElementById(this.canvasID).getContext("2d");
            this.ctx.strokeStyle = "rgb(0, 0, 0)";
            this.ctx.fillStyle = "rgba(255, 255, 255, 0.65)";
            this.ctx.lineWidth = 2.2;
            this.roundRect(this.ctx, 5, 5, this.width-10, this.height -10, 15);

            // attach events
            var e = document.getElementById(this.containerID);
            e.addEventListener("click", this.onClick, false);
        }
    }

    this.zoomIn = function()
    {
        alert(this.filename); // THIS IS THE PROBLEM, I WANT TO ACCESS INSTANCE VARIABLE FROM THIS FUNCTION
        $("#theGarden").jsMovie("destroy");

        $('#theGarden').jsMovie({
            sequence: filename,
            from: from,
            to: to,
            folder : folder,  
            showPreLoader : false,
            repeat : false,
            fps: 10,
            loadParallel: 1,
            width: 960,
            height: 529
        });
        $("#theGarden").jsMovie('play',0,15);
        //$("#theGarden").jsMovie("gotoFrame",to);
        //$("#theGarden").jsMovie("pause");
    }

    this.onClick = function(f)
    {
        this.zoomIn(); // THIS IS THE INTERNAL CALL THAT FAILS
    }

    function supports_canvas() 
    {
        return !!document.createElement('canvas').getContext;
    }

    this.roundRect = function(ctx, x, y, w, h, r) 
    {
        var mid = w/2;
        var baseOff = 10;
        var offh = h - baseOff;

        ctx.beginPath();
        ctx.moveTo(x + r, y);
        ctx.lineTo(x + w - r, y);
        ctx.quadraticCurveTo(x + w, y, x + w, y + r);
        ctx.lineTo(x + w, y + offh - r);
        ctx.quadraticCurveTo(x + w, y + offh, x + w - r, y + offh);

        ctx.lineTo(x + mid + 10, y + offh);
        ctx.lineTo(x + mid, y + h);
        ctx.lineTo(x + mid - 10, y + offh);
        ctx.lineTo(x + r, y + offh);

        ctx.quadraticCurveTo(x, y + offh, x, y + offh - r);
        ctx.lineTo(x, y + r);
        ctx.quadraticCurveTo(x, y, x + r, y);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();

    }
}
  • 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-23T07:16:19+00:00Added an answer on May 23, 2026 at 7:16 am

    If you’re setting up that “onClick” function as an event handler, with code something like this:

      someElement.onclick = thing.onClick;
    

    then the problem is that when an actual call to the function is made (when an event occurs), the browser won’t be able to arrange for this to be the right thing. Thus, when you set up the event handler, you have to make sure you bind a function that does know the context:

      someElement.onclick = function() { thing.onClick(); };
    

    edit — Oh ok, now I see the code that binds the handler. That should look like this:

            // attach events
            var e = document.getElementById(this.containerID);
            var thisObject = this;
            e.addEventListener("click", function() { thisObject.onClick(); }, false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this question is very simple: I have a facebook page, and a website.
Okay I've spent the last hour trying to find a solution to this but
Hye guys. Okay. I have done this coding. But it seems have error. Can
Okay guys, basically the problem I'm having is this. I've been assigned to write
Okay, before you guys go nuts -- this is just a small site, temporary
Okay, here goes. Stack Overflow virgin here but hopefully you guys will be able
Okay, I feel a bit foolish for having to ask this but I guess
Okay, some Guys will know what i mean and edit my Question but they
Okay guys, maybe you can help me out with this one. I'm about ready
Okay, I'll be straight with you guys: I'm not sure exactly how Domain Driven

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.