2016-04-07 9 views
4

So habe ich eine Datenstruktur wieKann ich in diesem Zusammenhang auf den Schlüssel verweisen?

this.PauseFunctions = { 
     2: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the 2-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the 2-second mark was called"); 
      } 
     }, 
     5: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the 5-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the 5-second mark was called"); 
      } 
     } 
    }; 

und ich frage mich, wenn ihr möglich für mich auf den Schlüssel, dessen Wert zu beziehen ist das Objekt, mit dem OnSlideTo Funktion. Zum Beispiel in

 2: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the 2-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the 2-second mark was called"); 
      } 
     } 

Ich frage mich, ob es einen Weg gibt es

 2: { 
      OnSlideTo: function() { 
       console.log("The OnSlideTo function of the event at the " + key + "-second mark was called"); 
      }, 
      OnSlideAway: function() { 
       console.log("The OnSlideAway function of the event at the " key + "-second mark was called"); 
      } 
     } 

zu

zu ändern, in dem key 2 ist so, dass mein Programm allgemeinere und wartbar sein.

+1

, dass jedes Objekt ausgewertet wird nicht möglich, da der Weg ist. –

+0

Warum brauchen Sie Baumfunktionen wo sind sie dann? – Amit

Antwort

3

Sicher, aber Sie müssen es tun, nachdem die Werte definiert sind und dann den Schlüsselwert säen. Es sollte nicht zu kompliziert sein, nur eine einfache for-Schleife, sobald die Schlüssel gefüllt sind.

var temp = new function(){ 
 

 
    this.PauseFunctions = { 
 
     2: { 
 
      //Key:, 
 
      OnSlideTo: function() { 
 
       console.log("The OnSlideTo function of the event at the "+this.Key+"-second mark was called"); 
 
      }, 
 
      OnSlideAway: function() { 
 
       console.log("The OnSlideAway function of the event at the "+this.Key+"-second mark was called"); 
 
      } 
 
     }, 
 
     5: { 
 
      //Key:, 
 
      OnSlideTo: function() { 
 
       console.log("The OnSlideTo function of the event at the "+this.Key+"-second mark was called"); 
 
      }, 
 
      OnSlideAway: function() { 
 
       console.log("The OnSlideAway function of the event at the "+this.Key+"-second mark was called"); 
 
      } 
 
     } 
 
    }; 
 

 
    //seed key value 
 
    for(var key in this.PauseFunctions){ 
 
     this.PauseFunctions[key].Key = key; 
 
    } 
 
}; 
 
temp.PauseFunctions[2].OnSlideTo();

Verwandte Themen