0

A few of my colleagues and I have been experimenting with returning bound functions from constructors.

This allows us to make these functions that 'build' other functions, without calling them buildDispatcher or prepareAddition. Let me give an example:

function addition(a, b) {
  return a + b;
}

function buildOffsetter(a) {
  return (b) => addition(a, b);
}

var offsetter = buildOffsetter(42);

console.log(offsetter(10));

If I were to use a constructor instead, I could do this instead:

function addition(a, b) {
  return a + b;
}

function Offsetter(a) {
  return (b) => addition(a, b);
}

var offsetter = new Offsetter(42);

console.log(offsetter(10));

Now, a more real-life example might look like this:

function dispatchDataToUrl(url, data) {
  $.ajax({
    type: "POST",
    url: url,
    data: data,
    error: () => console.log(`An error happened when sending [${data}] to [${url}].`) 
  });
}

function buildDispatcher(url) {
  return (data) => dispatchDataToUrl(url, data);
}

var dispatcher = buildDispatcher("http://stackoverflow.com");

dispatcher("stackoverflow rocks");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

which could be built like this:

function dispatchDataToUrl(url, data) {
  $.ajax({
    type: "POST",
    url: url,
    data: data,
    error: () => console.log(`An error happened when sending [${data}] to [${url}].`) 
  });
}

function Dispatcher(url) {
  return (data) => dispatchDataToUrl(url, data);
}

var dispatcher = new Dispatcher("http://stackoverflow.com");

dispatcher("stackoverflow rocks");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

So, we really like not having these "build"-functions that return bound functions.

My question is, is there any downside to this? Is there some hidden pitfall that we haven't stumbled into yet?

4
  • Resource wastage aside (see georg's answer/MDN), when I see new Something(), I'm expecting an object, not a function. The builder functions are much clearer to me. Commented Nov 17, 2016 at 13:03
  • But isn't that merely a question of habit? Resource wastage aside. Habits are as habits are. If you're used to seeing specific patterns, other patterns might seem less clear until you are used to seeing those. Commented Nov 17, 2016 at 13:54
  • 1
    Yeah, of course it's a stylistic choice - not trying to imply you're wrong to do things this way! I just don't see the value, personally. Commented Nov 17, 2016 at 13:59
  • I understand your intention. :) Commented Nov 17, 2016 at 14:01

1 Answer 1

3

x = new Something() where Something returns an object is exactly* the same as x = Something() except that new wastes a few resources allocating a new object which is ultimately thrown away. So the only actual difference between your buildOffsetter and Offsetter is their naming. As to downsides, allocating memory you're not going to use is a bad idea.


* Provided Something is a constructor, and you don't rely on its this.

Sign up to request clarification or add additional context in comments.

1 Comment

I usually find it harder to have good naming, than keeping a low memory footprint. Didn't Phil Karlton say something clever about naming once? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.