如何使用字符串访问对象属性?

时间:2016-02-26 00:09:30

标签: javascript object

如何在this.resources.language函数中访问setLanguage(),其中属性当前语言是资源中的四种之一?

myLanguage = new Language( 'en' );

function Language (currentLanguage) {
  this.currentLanguage = currentLanguage;
  this.resources = {
    en: {
        latest_songs_heading: "Latest",
        popular_songs_heading: "Popular",
    },
    de: {
        latest_songs_heading: "Neueste",
        popular_songs_heading: "Beliebteste",
    },
    es: {
        latest_songs_heading: "Más reciente",
        popular_songs_heading: "El mas popular",
    },
    po: {
        latest_songs_heading: "Mais recentes",
        popular_songs_heading: "O mais popular",
    }
  },
  this.getLanguage = function() {
    return 'Current language: ' + this.currentLanguage;
  }
  this.setLanguage = function() {
    var language = this.currentLanguage;
    return this.resources.language;
  }
}

1 个答案:

答案 0 :(得分:3)

bracket notation

return this.resources[language];