Ad
  • Custom User Avatar

    Hi,

    I can't quite get my head around this. I understand that you loop through the proterties and you need to reference to the constructor's arguments.

    But why is that? Why wouldn't it work to assign the property-values as proterties to the object?

    How does the contructor function receive the passed property arguments from the wrapper function?

    function makeClass(...properties) {
    
      //WRONG: referenecing wrapper arguments only
      
      return function() {
        properties.forEach((prop, i) => {
          return this[prop] = prop
        })
      }
      
      //RIGHT solution:
      
      return function() {
        properties.forEach((prop, i) => {
          return this[prop] = arguments[i]
        })
      }
    }
    

    And how do you add syntax-highlighting to your code blocks in your comments?

  • Custom User Avatar

    In this case the regular expression will ignore the underscore in the return value, since it cannot be paired with another character.