为什么我的财产无法读取“未定义”?

时间:2019-06-11 11:23:06

标签: javascript object this undefined

我创建了一个对象,其中一个键是一个函数。

如果我在对象外部进行初始化,那么一切都很好。但是在对象内部-错误

也许我在上下文中错了?请告诉我,我不知道该怎么想和阅读,但我仍然不明白问题是什么

还是.this问题?

import random

my_list = range(1,99) 
list_Odd_Numbers = list(filter(lambda varX: varX % 2 == 1,my_list))
while True: # Not recommended, but since you asked for infinite loop...
    x = random.choice(list_Odd_Numbers) # Pick randomly one number from the list.
    print(x) # or do whatever you need to with this random pick.

结果:

exports.Constants = {
  Number: {
    FIRST: {
      en: 'first',
      de: 'erste',
    },
    SECOND: {
      en: 'second',
      de: 'zweite',
    },
    THIRD: {
      en: 'third',
      de: 'dritte'
    }
  },
  NumbersLocal: ((numbersLocals) => {
    const getNumbers = (locale) =>
      ['FIRST', 'SECOND', 'THIRD'].map(number => numbersLocals[number][locale]);
    return {
      en: getNumbers('en'),
      ru: getNumbers('de')
    };
  })(this.Number),

};

但是我这样使用就可以了,

12332.js:18

      ['FIRST', 'SECOND', 'THIRD'].map(number => numbersLocals[number][locale]);
                                                              ^

TypeError: Cannot read property 'FIRST' of undefined
    at map.number

结果:

exports.Constants = {
  Number: {
    FIRST: {
      en: 'first',
      de: 'erste',
    },
    SECOND: {
      en: 'second',
      de: 'zweite',
    },
    THIRD: {
      en: 'third',
      de: 'dritte'
    }
  },
};

this.Constants.NumbersLocal = ((numbersLocals) => {
  const getNumbers = (locale) =>
    ['FIRST', 'SECOND', 'THIRD'].map(number => numbersLocals[number][locale]);
  return {
    en: getNumbers('en'),
    ru: getNumbers('de')
  };
})(this.Constants.Number);

console.log(this.Constants.NumbersLocal);

0 个答案:

没有答案