我的构造函数有什么问题

时间:2018-03-12 15:52:13

标签: javascript function constructor return

我的构造函数遇到问题并需要帮助,这是我到目前为止所拥有的...



function EgyptianGoddesses(name, oversees, strength, weakness) {
  this.name = name;
  this.oversees = oversees;
  this.strength = strength;
  this.weakness = weakness;

  return function() {
    name,
    oversees,
    strength,
    weakness
  }
  
}

var maatCharacteristics = ['Maat', 'Truth and Justice', 'Balancing and Just', 'Exacting in her standards']

var maat = new EgyptianGoddesses(this.maatCharacteristics)
console.log(maat)




我只是得到一个空的构造函数,我认为返回有问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

此:

var maat = new EgyptianGoddesses(this.maatCharacteristics)

应该传递来自变量的参数,而不是属性,并且应该将它们展开。

var maat = new EgyptianGoddesses(...maatCharacteristics)

现在maatCharacteristics的各个成员将被传递给构造函数的各个参数。

不确定为什么你会返回一个什么都不做的功能。您需要解释您的意图,以便妥善修复。