Search code examples
javascriptecmascript-5

Function as property of another function ES5


I haven't long written es5, so I forgot. Can you help how to fix this?

let mock = {
  DynamoDB: function() {
    {
        send: function() {console.log('sending...')}
    }
  },
};

then not working.

let client = new mock.DynamoDB();
client.send(); // does not write to console

Solution

  • A constructor function should assign to a property of this.

    let mock = {
      DynamoDB: function() {
        this.send = function() {
          console.log('sending...')
        }
      }
    };
    
    let client = new mock.DynamoDB();
    client.send();