1
function Meal(n) {
    function private(cal) {
        return cal * 2;
    }
    this.calories = private(n);
}

//private is being reinstantiated three times?
var breakfast = new Meal(3);
var lunch = new Meal(56);
var dinner = new Meal(100);

Is the private function an unnecessary performance hit? Would it be the same as:

function public(cal) {
    return cal * 2;
}

function Meal(n) {
    this.calories = public(n);
}

MDN writes:

The object returned by the constructor function becomes the result of the whole new expression.

I'm having some trouble understanding "the result of the whole new expression." Wouldn't that mean that, because private function declarations are within the constructor definition, they are a "result of the whole new expression"? It seems like javascript would be smart enough not to reinstantiate private functions like this but I'm not sure.

2
  • "It seems like javascript would be smart enough not to reinstantiate private functions like this" You'll probably find at that level that it's an implementation detail of the JS engine involved. Commented Feb 10, 2016 at 14:44
  • I'm sure modern Javascript implementations optimise the crap out of that. If this was a real practical concern we'd be writing everything as a global function, which obviously is pretty pointless. Use Javascript scoping rules to write what you mean first and foremost. Commented Feb 10, 2016 at 14:45

1 Answer 1

4

Are private function declarations within a constructor reinstantiated everytime an instance is created?

Yes, a new function object is created every time. The underlying code may be shared between them if the engine is smart enough. V8 (in Chrome) is, and I'm sure most if not all the top-flight engines are here in 2016.

For that matter, given the example private function, what it does, and its scope, I'd hope a decent engine would optimize the function away entirely.

Is the private function an unnecessary performance hit?

That's your call. Creating functions is very, very fast on any modern JavaScript engine.

In that particular case, the private you've shown isn't using any information that's only available inside your constructor, so there's no particular reason to create it in the constructor. Moving it out makes sense. You can still keep it private if you like, without recreating it every time:

var Meal = (function() {
    function private(cal) {
        return cal * 2;
    }

    return function Meal(n) {
        this.calories = private(n);
    };
})();

Now, private is only created once, and that single function is used by all Meal instances. (Meal is also only created once.)

The object returned by the constructor function becomes the result of the whole new expression.

That's mostly unrelated to your main question. When you do new SomeFunction, the JavaScript engine creates an object backed by the object that SomeFunction.prototype refers to, and then calls SomeFunction with this referring to that object. If SomeFunction doesn't return anything, or returns a primitive like 42, or returns null, the result of the new expression is the reference to the object that new created. If SomeFunction returns a non-null object reference, that's the result of the new expression instead.

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

Comments

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.