简体   繁体   中英

Javascript Prototype make function that returns value from array

I recently started to learn prototype and wanted to ask if there is some way to make something like this but in working shape, if I use this.lang[array][keys][here] or something similar it would return value from this.defaults.translations reason behind this is, I wan't to make lang function in which it will check if given index exists and if no then it will fallback on default language.

function Test() {
   this.output();
}

Test.prototype.defaults = {
   lang: 'en',
   translations: {
      en: {
         something: [
            'Something here'
         ]
      }
   }
};

Test.prototype.lang = function() {
    return this.defaults.translations[this.defaults.lang];
};

Test.prototype.output = function() {
   return this.lang['something'][0];
};

var test = new Test();

UPDATED: Found solution, check my answer below!

You need to call lang (or use a getter).

 function Test() {} Test.prototype.defaults = { lang: 'en', translations: { en: { something: [ 'Something here' ] } } }; Test.prototype.lang = function() { return this.defaults.translations[this.defaults.lang]; }; Test.prototype.output = function() { return this.lang()['something'][0]; }; console.log(new Test().output()); // 'Something here' 

Just use a getter:

Test.prototype.getLang = function getLang(lang) { 
  return this[lang] || someDefaultLang;
};

var test = new Test();
console.log(test.getLang("en"));

If you want to be fancier and don't need to support old IE you can alternately do this:

Object.defineProperty(Test.prototype, "lang", {
  get: function(lang) {
    return this[lang] || someDefaultLang;
  }
});

console.log(test.en);

works in IE 9+

You just do it like this;

 function Test() {}; Test.prototype.defaults = { lang: 'en', translations: { en: { something: [ 'Something here' ] } } }; Test.prototype.lang = function() { return this.defaults.translations[this.defaults.lang]; }; Test.prototype.output = function() { return this.lang(); }; var test = new Test(); console.log(test.output()); 

It is very much depends on your future needs, but one other option is using static properties:

  function Test() { } Test.defaults = { lang: 'en', translations: { en: { something: [ 'Something here' ] } } }; Test.prototype.lang = function() { return Test.defaults.translations[Test.defaults.lang]; }; Test.prototype.output = function() { return this.lang()['something'][0]; }; var test = new Test(); alert(test.output()); 

Found solution, there is what I did to manage what I wanted.

 function Test() {} Test.prototype.defaults = { lang: 'en_GB', fallbackLang: 'en_US', translations: { en_US: { strings: [ 'Color', 'Chips' ] }, en_GB: { strings: [ 'Colour', 'Crisps' ] } } }; Test.prototype.splitToArray = function(array, string) { Array.prototype.forEach.call(string.split('.'), function(key) { if (typeof array !== "undefined") { array = array[key]; } }); return array; }; Test.prototype.__ = function (key) { var self = this, translation = this.splitToArray(self.defaults.translations[self.defaults.lang], key); if (typeof translation === "undefined") { // Set to fallback language if translation is not found translation = this.splitToArray(self.defaults.translations[self.defaults.fallbackLang], key); if (typeof translation === "undefined") { // return given key if translation is not found return key; } } return translation; }; console.log(new Test().__('strings.0')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM