0

I have this code snipet:

proxy('http://my-custom-api-endpoint.com', {
  proxyReqOptDecorator(options) {
    options.headers['x-forwarded-host'] = 'localhost:3000'
    return options
  }
})

It is a call to a function named proxy, the first argument is a string, but the second argument has a syntax than I can't recognize:

{
  functionName(args) {
    // statements
  }
}

Can someone explain that syntax please?

Charlie
  • 22,886
  • 11
  • 59
  • 90
Alejo Dev
  • 2,290
  • 4
  • 29
  • 45

1 Answers1

3

Its a shorthand method in Object Initializer to create a property whose value is a function.

// Shorthand method names (ES2015)
let o = {
  property(parameters) {}
}
//Before
let o = {
  property: function(parameters) {}
}

This syntax is also sued in classes to declare class methods.

class Animal { 
  speak() {
    return this;
  }
  static eat() {
    return this;
  }
}class Animal { 
  speak() {
    return this;
  }
  eat() {
    return this;
  }
}
Charlie
  • 22,886
  • 11
  • 59
  • 90
  • Since your class example includes both static methods and uses this, you might want to clarify some of the differences or just use instance methods in the class example – Aluan Haddad Aug 08 '20 at 19:20