1

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 everytime the constructor gets called. Does this happen with the function declaration aswell, 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.)

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

EDIT: 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".

Community
  • 1
  • 1
Wingblade
  • 8,065
  • 9
  • 30
  • 41

3 Answers3

1

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.

Paul S.
  • 58,277
  • 8
  • 106
  • 120
  • So there's no difference outside of the usual function declaration vs expression differences, then? And I'm aware that javascript doesn't have real classes, I'm using the term "private method" loosely. – Wingblade Oct 28 '15 at 00:05
  • Yep. If it's not necessary to be an _expression_ I usually write them as _declarations_ because personally I find them easier to read. I do write expressions a lot though. – Paul S. Oct 28 '15 at 00:12
0

Virtually no difference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#Function_constructor_vs._function_declaration_vs._function_expression

More importantly, they aren't private methods (which don't exist in JavaScript). The methods you created are only accessible inside the constructor, extremely limiting their value.

Adam
  • 41,349
  • 10
  • 58
  • 78
0

the major difference is

function declaration get hoist in it's enclose scope lets say

var Klass = function() {

   return {
     publicMethod: privateMethod
   };

   function privateMethod() {

   }
}

you can reference privateMethod below the return, because privateMethod will be hoisted in function Klass, but you can't do this with function expression

var Klass = function() {

   return {
     publicMethod: privateMethod    // privateMethod will be undefined
   };

   var privateMethod = function () {

   }
}

you can not ref the name of the method with function expression that is said

var method = function() {
  if(somecondition) {
    method();                         
  }
}

you have to give it a name, always perfer named method

var method = function method() {
  if(somecondition) {
    method();
  }
}
Sean
  • 2,602
  • 1
  • 16
  • 29