I'm not having a problem, not trying to fix anything. I'm just curious why Javascript works this way. I've poked around with google, but "js function no name" gets a lot of hits about how to define and use anonymous functions (not what I'm looking for). And there's hardly anything out there about declaring functions with the syntax that is causing my confusion--I don't even know what that syntax is called.
The issue: I'm trying to figure out why declaration syntax has any effect on the function name when the function is inside an object. If I declare an object with a function inside like this:
var objectOne = { apple: function() {} }
The apple()
function gets a name. That is, console.log(objectOne.apple.name)
displays "apple".
But if I declare an object with a function inside like this:
var objectTwo = {}
objectTwo.banana = function() {}
Then banana()
doesn't get a name. That is, console.log(objectTwo.banana.name)
displays nothing. Same for the following, and similar permutations.
var objectThree = { catapult: null }
objectThree.catapult = function() {}
I can name the functions explicitly, of course, but again, I'm not trying to fix anything, I just wonder why the two syntaxes produce different results. I've always thought of them as functionally interchangeable.
I notice that both of these forms, not inside an object, get names automatically:
function one() {}
var two = function() {}
Why is the object.property = function(){}
form treated differently from the other forms?