Accessing "Public" methods from "Private" methods in javascript class

Posted by mon4goos on Stack Overflow See other posts from Stack Overflow or by mon4goos
Published on 2010-04-24T07:13:56Z Indexed on 2010/04/24 7:23 UTC
Read the original article Hit count: 483

Is there a way to call "public" javascript functions from "private" ones within a class?

Check out the class below:

function Class()
{
    this.publicMethod = function()
    {
        alert("hello");
    }

    privateMethod = function()
    {
        publicMethod();
    }

    this.test = function()
    {
        privateMethod();
    }
}

Here is the code I run:

var class = new Class();
class.test();

Firebug gives this error:

publicMethod is not defined: [Break on this error] publicMethod();

Is there some other way to call publicMethod() within privateMethod() without accessing the global class variable [i.e. class.publicMethod()]?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about object-oriented-design