1

I have a javascript function (function1) that checks some global variables (can the user connect, is the service available, etc) before calling another (legacy) function (function2) that actually pops a window and connects the user to our service. I want to prevent function2 from being called anywhere but from function1. Is this possible?

As a cheap solution, I figured I could emit a variable from function1 and check for it in function2 before executing. Is there another way? Is there a way to find out the calling element/method in a javascript function?

rmw
  • 1,253
  • 1
  • 12
  • 20
  • while declaring function2 is a valid solution, this is actually a simlified version of my problem. there actually multiple function2s and it seems somewhat messy to declare them all inside function1. that's why i marked the answer using arguments.callee.caller. thanks all! – rmw Feb 06 '09 at 20:04

5 Answers5

4

Read here: Crockford's method. Declare the function inside the first function.

jacobangel
  • 6,896
  • 2
  • 34
  • 35
2

You should check this:

and this:

Community
  • 1
  • 1
Tiago
  • 9,457
  • 5
  • 39
  • 35
0

Why not just remove function2, move its entire content into function 1 ?

Then you can just replace the old function 1 with something that signals the error.

krosenvold
  • 75,535
  • 32
  • 152
  • 208
0

you can move function2 inside of function1 so that's it's "private" to function1. check out the "module pattern".

Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58
0
var function1 = function() {
    var function2 = function() { alert('hi'); };

    function2();
};

function1(); // alerts "hi";

function2(); // error function2 is undefined
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169