javascriptfunctionfunction-expressionjavascript-function-declaration

Javascript private methods: function expression vs function declaration


A common method of creating private methods (of sorts) in JavaScript is this:

Class = function (arg0, arg1) {
    var private_member = 0;
    var privateMethod = function () {
        return private_member;
    };
}

The above example could also have been done with a function declaration instead of a function expression:

Class = function (arg0, arg1) {
    var private_member = 0;
    function privateMethod () {
        return private_member;
    }
}

In what ways are these two ways of declaring a private method different? (Outside of one being an expression and the other being a declaration)

For example, the expression obviously generates a new function every time the constructor gets called. Does this happen with the function declaration as well, or does it only get evaluated once because function declarations are evaluated at parse time? (As opposed to function expressions which are evaluated at execution time, you get the idea.)

I'm aware that JavaScript doesn't really have private methods. I'm using the the term loosely.

Not a duplicate of var functionName = function() {} vs function functionName() {}, if anything a duplicate of function expression vs function declaration with regard to javascript 'classes'. My question isn't about the difference between function expressions and function declarations in general, but their differences specifically in regards to "private members" in JavaScript "classes".


Solution

  • the expression obviously generates a new function everytime the constructor gets called. Does this happen with the function declaration aswell [...]?

    A new function will be generated every time by either method

    because function declarations are evaluated at parse time

    Function declarations are hoisted similarly to how vard identifiers are hoisted. This happens at the beginning of invocation of the scope they're in

    In what ways are these two ways of [defining a] method different?

    This is the same as elsewhere, you can reference a function declaration on lines before the line it is defined (assuming it's in your scope)

    foo.bar; // undefined
    function foo() {}
    

    vs the function expression hasn't been hoisted, so the identifer foo is referenceable but you can't reference the function you will have assigned to it until after

    foo.bar; // TypeError: Cannot read property 'bar' of undefined
    var foo = function () {};
    // same as
    var foo;
    foo.bar; // TypeError: Cannot read property 'bar' of undefined
    foo = function () {};
    

    Please ensure that you understand that JavaScript is not the same as classical programming languages; the words public and private are used by classical programmers when talking about JavaScript to mean "externally Referenceable" and "not externally Referenceable", respectively.
    Similarly, there isn't really such a thing as a Class (even in ES 6, it's just syntactic sugar for what we did already in ES 5). Instead we have Constructors and prototype chains.