0

I have several global objects that contain arrays of objects; let's call these appointments. Let's say I have these objects: AppointmentsToday, Appointments02102012, Appointments02092012... I also have a global variable CurrentAppointments that holds a reference to the actual object I'm currently working on.

The code looks somewhat like this:

var CurrentAppointments = new Object();

var AppointmentsToday = new Object();
var Appointments02102012 = new Object();
var Appointments02092012 = new Objects()

ProcessNewAppointments(TheAppointments) {
  CurrentAppointments = TheAppointments;    
}

SomeFunctionThatDoesWork() {
  .... Some code to get the array index we want to work with
  CurrentAppointments[i].MyProp = SomeValue;
  ProcessAppointments(WHAT DO I PUT THERE??);
}

CallingFunction() {
   ProcessNewAppointments(Appointments02102012);
}

For instance, when I write ProcessNewAppointments(Appointments02102012) CurrentAppointments points to Appointments02102012 and when the function SomeFunctionThatDoesWork gets called, it changes the values of array Appointments02102012. So basically, when the function SomeFunctionTheDoesWork() gets called, it works on the object that's currently in CurrentAppointments, without worrying about which object to work on.

Now what I want to do is reexecute ProcessNewAppointments after SomeFunctionThatDoesWork gets called. How can I know the name of the object CurrentAppointments is pointing to so that I can write something like ProcessNewAppointments(TheNameOfTheObjectCurrentlyInCurrentAppointments).

Thanks for your suggestions.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • 1
    I've read it 3 times and it is still confusing :), please clarify ... – mfeineis Feb 15 '12 at 15:50
  • How can I get the name of the object CurrentAppointments is pointing to? I just made some edits to clarify. – frenchie Feb 15 '12 at 15:52
  • the code in your example is totally invalid and "i" does not exist. is "ProcessNewAppointments" supposed to be a function or some construct of a more complex object? – Mark Schultheiss Feb 15 '12 at 15:57
  • @MarkSchultheiss: no, CurrentAppointments is an array of objects and i is the index we're working on. – frenchie Feb 15 '12 at 16:00

2 Answers2

0

It's still not clear to me, but here is what I understand your situation:

// You have some arrays ...
var AppointmentsToday = [ /* ... */ ], AppointmentsSomedate = [ /* ... */ ];

// Then you hold the current appointment in a global
var currentAppointment = {}; // better style than new Object()

// This chooses the current appointment
function ProcessNewAppointments(theAppointments) {
    currentAppointments = theAppointments;    
}

// This dispatches the current appointment
function SomeFunctionTheDoesWork() {
    CurrentAppointments[i].MyProp = SomeValue;
}

// ...

// At some time you call processNewAppointments(someGlobalArray)
SomeFunctionTheDoesWork();

// and then what?
mfeineis
  • 2,607
  • 19
  • 22
0

One fact here: javascript objects can be reffered to by multiple names. Given that, your question is a bit perplexing.

See this question for some discussion: How to get class object's name as a string in Javascript?

Perhaps a better method would be to send the function the object as a parameter.

My example code below, would alert "apropdiffhouse" twice for instance.

working example: http://jsfiddle.net/MarkSchultheiss/pCtaH/

var appointment = {
    Id: "newId",
    Name: "newN",
    MyProp: "newP"
};
var CurrentAppointments = {
    appoint: [appointment]
};
CurrentAppointments[0] = appointment;
CurrentAppointments[1] = appointment;
CurrentAppointments[2] = appointment;


var Appointments02092012 = {
    appoint: [appointment, appointment, appointment]
};

Appointments02092012[1] = {
    Id: "myid",
    Name: "myName",
    MyProp: "aprop"
};
var AppointmentsToday = {};
var Appointments02102012 = {};
var Appointments02092012 = {};
Appointments02102012[1] = appointment;
Appointments02102012[1] = {
    Id: "mynewid",
    Name: "mynewName",
    MyProp: "apropdiff"
};

function ProcessNewAppointments(TheAppointments) {
    CurrentAppointments = TheAppointments;
}

function SomeFunctionThatDoesWork(workingAppointment) {
    var SomeValue = "house";
    var i = 1;
    CurrentAppointments = workingAppointment;
    CurrentAppointments[i].MyProp = CurrentAppointments[i].MyProp +SomeValue;
    ProcessNewAppointments(workingAppointment);
}

function CallingFunction() {
       ProcessNewAppointments(Appointments02102012);
}
CallingFunction();
var currentIndex = 1;
SomeFunctionThatDoesWork(CurrentAppointments);
alert(CurrentAppointments[currentIndex].MyProp);
alert(Appointments02102012[currentIndex].MyProp);
Community
  • 1
  • 1
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100