0

I have an object that is defined in another file that I cannot edit. I need to call a function that was defined within it, but was not set as a method. My test does not work since callMe does not exist in my scope.

// Example
function ObjectCreator()
{
    this.name = "I am an object";


    var callMe = function()
    {
        console.log("You did it");
    }

    this.DontCallMe = function()
    {
        console.log("You should not have called me");
        callMe();
    }

    return this;
}

My File

// My attempt

var foo = new ObjectCreator();

foo.test = function()
{
    // throws not defined error
    callMe();
};

foo.test();

How can I call callMe() without editing the original file to make it public?

John
  • 5,942
  • 3
  • 42
  • 79

1 Answers1

1

It isn't possible. This function will be defined only inside the constructor, or other scopes nested on it - this how lexical scope works.

See: Getting All Variables In Scope

Edmundo Santos
  • 8,006
  • 3
  • 28
  • 38