Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it bad to use a function without parentheses in production? [closed]

Tags:

javascript

Reading through Invoking a function without parentheses it is stated multiple times in the comments and answers to not use such code in production. Why please?

I am a beginner at JavaScript as you can guess from the question. If someone could phrase their answer in layman's terms that would be great, though please also counter for the experienced JS folks among you that might need a more detailed and technically detailed reply.

Examples of what could or does go wrong with using functions without parentheses in production would be a great addition to an answer.

Here is example code of invoking functions without parentheses taken from the answers to that question.

var h = {
  get ello () {
    alert("World");
  }
}

Run this script just with:

h.ello  // Fires up alert "world"

or

var h = {
  set ello (what) {
    alert("Hello " + what);
  }
}

h.ello = "world" // Fires up alert "Hello world"
like image 985
lowtechsun Avatar asked Mar 12 '16 13:03

lowtechsun


People also ask

What happens when you call a function without parentheses?

Without parentheses, the function name is used as the pointer of the function. The name of a function is the pointer of the function. At this time, the result of the function is not obtained, because the function body code will not be run.

Do functions need parentheses?

Without parentheses you're not actually calling the function. A function name without the parentheses is a reference to the function. We don't use the parentheses in that code because we don't want the function to be called at the point where that code is encountered.

Do you need parentheses to call a function in Python?

Invoking FunctionsParentheses are necessary when you want to invoke functions. Calling on the name of a function without following it by parentheses will point towards the function object, but will not call the function itself. The code inside the body of the function will not get executed.

Can you call a function without parentheses Javascript?

Method 1: Using the new operator: The new operator is used to create an instance of an object which has a constructor function. This constructor function can be used to write our own function and then be invoked by the new operator.


1 Answers

It doesn't really matter whether it's in production or not.

It's because it doesn't look like a function call and that's confusing for other developers.

Imagine how difficult it might be to track down some complex behaviour in a large application when any property access and assignment could be calling out to some arbitrary code.

Although there are valid uses for getters, setters and eventually proxies, they are suited to very specific behaviours in small surface APIs. They probably shouldn't ever be used as a general programming technique.

like image 198
Dan Prince Avatar answered Oct 17 '22 02:10

Dan Prince