2

In a project where I am working(Vue.JS Project) I found in so many places they have used this 'get' before the function, but I am not clear yet why do we need that. I have added one function with this get:

  get dataNotYetArrived(): boolean {
    return justAnExample;
  }

It will be helpful if someone can explain this to me. Thanks

Mithun Das
  • 435
  • 4
  • 8
  • 1
    Does this answer your question? [What is the "get" keyword before a function in a class?](https://stackoverflow.com/questions/31999259/what-is-the-get-keyword-before-a-function-in-a-class) and [What this “get” in JavaScript object means?](https://stackoverflow.com/questions/7401048) – adiga Jul 13 '20 at 15:39
  • Not exactly the same one but I got some valuable idea after watching that post. Thanks mate. – Mithun Das Jul 13 '20 at 15:44
  • `get` is a javascript keyword nothing specific to Vue – adiga Jul 13 '20 at 16:19

1 Answers1

2

It's the getter syntax. It's a Javascript feature that assigns a function to be executed when accessing the property -- which is useful when you want the property to return something dynamic, rather than a static value. So:

get someProperty() { ... }

executes the function someProperty() when you access myInstance.someProperty.

More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

Matt Howell
  • 15,750
  • 7
  • 49
  • 56