0

I create objects using two different notations and then console.log them:

object = {key: 'value', func: function() {}};
console.log(object);

output: { key: 'value', func: [Function: func] }

object.key = 'value';
object.func = function() {};
console.log(object);

output: { key: 'value', func: [Function] }

Why there's a difference in the outputs? Does it matter?

adiga
  • 34,372
  • 9
  • 61
  • 83
vfinn
  • 140
  • 1
  • 8

1 Answers1

2

This has nothing to do with dot notation and bracket notation.

In example (a) you are declaring a property name (func) and value (a function) in an object literal. In example (b) you are assigning a function to a property afterwards.

In the first case, func is used as a Binding Identifier which is used to give the function a name.

That isn't the case in the second example.

This isn't likely to make much difference, but function names can be useful when debugging. You can give a function expression a name explicitly:

object.func = function func () {};
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Ok, but I think it would make sense, even if there were no function name associated in the first case either, then. – vfinn May 27 '21 at 13:31