0

Assume we have the following object

const foo = {
  bar: "hello "
}

Is it possible to achieve the following result:

console.log(foo.bar) // logs "hello "
console.log(foo.bar("world")) // logs "hello world"

I want to be able to get or call the same property and apply a function if called

I have tried using Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy with apply() handler, however I was not able to find a working solution, every attempt ends at This expression is not callable. Type 'String' has no call signatures.

deithy
  • 118
  • 6
  • What are you trying to achieve with this? What’s the end goal? – Cristik Jan 28 '23 at 04:17
  • I am trying to satisfy my curiosity, to my knowledge It's impossible, but perhaps I'm missing something – deithy Jan 28 '23 at 04:20
  • 1
    Does this answer your question: [Differentiate between property and method with same name](https://stackoverflow.com/a/29435958/1974224)? – Cristik Jan 28 '23 at 04:25

0 Answers0