0

I am having trouble with my constructor function and need help, here is what I have so far...

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)

I am just getting an empty constructor, and I think it is something wrong with the return. Any help would be appreciated.

2
  • 3
    Don't return anything from the constructor, omit even the return statement. Commented Mar 12, 2018 at 15:53
  • 1
    new EgyptianGoddesses(this.maatCharacteristics) You don't want the this here, it works here only because this context is going to be global, and the var is currently global. Seen as maatCharacteristics is in scope, just do new EgyptianGoddesses(maatCharacteristics) Commented Mar 12, 2018 at 15:59

1 Answer 1

4

This:

var maat = new EgyptianGoddesses(this.maatCharacteristics)

Should pass the arguments from the variable, not a property, and should spread them out.

var maat = new EgyptianGoddesses(...maatCharacteristics)

Now the individual members of maatCharacteristics will be passed to the individual parameters of the constructor.

Not sure why you're returning a function that does nothing. You'll need to explain your intent for that to be properly fixed.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, I can not figure out when and when not to use the spread operator to save my life, and yes I didn't need to return it, not too familiar with constructor functions yet.
You're welcome. The spread is pretty simple. If you have a collection, and you want to pass the members of the collection as individual members, use the spread syntax. It also works with array literals, for example: var newArr = ["foo", "bar", ...maatCharacteristics] That creates a new array that includes the members of your original array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.