2012-02-15 3 views
0

私はオブジェクトの配列を含む複数のグローバルオブジェクトを持っています。これらの予定を呼びましょう。私はこれらのオブジェクトを持っているとしましょう:AppointmentsToday、Appointments02102012、Appointments02092012 ...私は現在作業している実際のオブジェクトへの参照を保持するグローバル変数CurrentAppointmentsを持っています。私はAppointments02102012にProcessNewAppointments(Appointments02102012)CurrentAppointments点を書き込み、機能SomeFunctionThatDoesWorkが呼び出されるとき、それはアレイAppointments02102012の値を変更したとき、例えばjavascriptでどのオブジェクトをポイントするかを調べる

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); 
} 

コードは多少このようになります。だから、基本的には、関数SomeFunctionTheDoesWork()が呼び出されると、現在のCurrentAppointmentsにあるオブジェクトに対して、どのオブジェクトを処理するか心配することなく動作します。

ここで私がやりたいことは、SomeFunctionThatDoesWorkが呼び出された後にProcessNewAppointmentsを再実行することです。どのように私はProcessNewAppointments(TheNameOfTheObjectCurrentlyInCurrentAppointments)のようなものを書くことができるように、CurrentAppointmentsが指しているオブジェクトの名前を知ることができます。

お寄せいただきありがとうございます。

+1

私はそれを3回読んだし、それはまだ混乱:)で、明確にしてください... – mfeineis

+0

はどのようにして、オブジェクトCurrentAppointmentsの名前を指している得ることができますか?私はちょうど明確にするためにいくつかの編集をしました。 – frenchie

+0

あなたの例のコードは完全に無効で、 "i"は存在しません。 "ProcessNewAppointments"は、より複雑なオブジェクトの関数または何らかの構成であると考えられていますか? –

答えて

0

一つの事実:オブジェクトは複数の名前によってrefferedすることができますJavaScriptを。それを考えると、あなたの質問はちょっと難解です。おそらく、より良い方法は、パラメータとして関数にオブジェクトを送信することですHow to get class object's name as a string in Javascript?

は、いくつかの議論のために、この質問を参照してください。

以下の例のコードでは、「apropdiffhouse」に2回警告が表示されます。

作業例: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); 
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? 
関連する問題