0

First of all, I recognize there are like 100+ threads on method.this vs var method, etc. That is not what I am asking. I know the differences. I am confused of function expressions VS function declarations in the context of creating objects using a constructor.

What I wanted to know is this: How come function declarations never appear in constructors, but function expressions do? Whenever I declare a function rather than use an expression it is not accessible in the object once initiated; why is this?

I think it is because either it is local (aka private) and thus inaccessible, or when creating objects, if the constructor does not see the function prefixed with 'this', 'var', or 'constructor.prototype', it skips it.

chopper draw lion4
  • 9,509
  • 12
  • 45
  • 85
  • There are hundreds of threads about [`this.method` vs `.prototype.method`](https://stackoverflow.com/questions/310870/use-of-prototype-vs-this-in-javascript), and hundreds of threads about [`var fn = function()` vs `function fn()`](http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1), but none about what you're talking about. There is no such thing as `var method`. Could you maybe more specific about what you already know? – Bergi Apr 14 '14 at 12:03

2 Answers2

4

How come function declarations never appear in constructors, but function expressions do?

They do

Whenever I declare a function rather than use an expression it is not accessible in the object once initiated; why is this?

You must be doing it wrong.

  function Thing() {
    function go() {
      alert('Hello');
    }
    this.foo = 1;
    this.bar = function () {
      go();
    }
  }

  var x = new Thing();
  x.bar();

I think it is because either it is local (aka private) and thus inaccessible

Function declarations are locally scoped. That doesn't stop you assigning them to a wider scope, to an object property or using them within the scope they are declared within.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
1

How come function declarations never appear in constructors, but function expressions do?

They do. You could easily replace every function expression with an equivalent function declaration.

Whenever I declare a function rather than use an expression it is not accessible in the object once initiated; why is this?

Because function declaration declare a function in the current scope, just as var declares a variable. See also Javascript: Do I need to put this.var for every variable in an object? for the difference between variables and properties.

if the constructor does not see the function prefixed with 'this', 'var', or 'constructor.prototype', it skips it.

Nope. It's not about "being seen prefixed with". You are writing the code, and the engine doesn't know what it is executing. A function expression by itself is nothing but an expression, it gathers a meaning when you do assign it somewhere. When you assign it to a property of the current instance (this), it becomes a method for example. The same can be done with a function declaration:

function Constructor() {
    // creating local identifiers:
    var variable = function expression(){ };
    function declaration() { };

    // exporting properties:
    this.method1 = variable;
    this.method2 = declaration;
    // or without the local variable:
    this.method3 = function expression() { };
}
Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • +1. I hadn't made the association between function "declaration" and variable scope before. It's obvious in hindsight. – Quentin Apr 14 '14 at 12:02