我有几个包含对象数组的全局对象;让我们称这些约会。假设我有这些对象:AppointmentsToday,Appointments02102012,Appointments02092012 ...我还有一个全局变量CurrentAppointments,它包含对我正在处理的实际对象的引用。
代码看起来有点像这样:
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);
}
例如,当我编写ProcessNewAppointments(Appointments02102012)时,CurrentAppointments指向Appointments02102012,当函数SomeFunctionThatDoesWork被调用时,它会更改数组Appointments02102012的值。所以基本上,当函数SomeFunctionTheDoesWork()被调用时,它对当前在CurrentAppointments中的对象起作用,而不用担心要处理哪个对象。
现在我要做的是在调用SomeFunctionThatDoesWork之后重新执行ProcessNewAppointments。如何知道CurrentAppointments指向的对象的名称,以便我可以编写类似 ProcessNewAppointments(TheNameOfTheObjectCurrentlyInCurrentAppointments)的内容。
感谢您的建议。
答案 0 :(得分:0)
我仍然不清楚,但这是我了解你的情况:
// 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?
答案 1 :(得分:0)
这里有一个事实:javascript对象可以被多个名称引用。鉴于此,你的问题有点令人困惑。
请参阅此问题进行讨论:How to get class object's name as a string in Javascript?
也许更好的方法是将对象作为参数发送给函数。
下面我的示例代码会提醒两次“apropdiffhouse”。
工作示例: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);