I have no problems, I'm not trying to fix anything. I'm just wondering why Javascript works this way. I poked using google, but "js function no name" gets a lot of views on how to define and use anonymous functions (not what I'm looking for). And almost nothing is said there about declaring functions with syntax causing my confusion - I donβt even know what this syntax is called.
Problem . I am trying to understand why the declaration syntax affects the function name when the function is inside the object. If I declare an object with a function inside as follows:
var objectOne = { apple: function() {} }
The apple() function gets the name. That is, console.log(objectOne.apple.name) displays "apple".
But if I declare an object with a function inside:
var objectTwo = {} objectTwo.banana = function() {}
Then banana() does not get the name. That is, console.log(objectTwo.banana.name) does not display anything. The same goes for the following and similar permutations.
var objectThree = { catapult: null } objectThree.catapult = function() {}
I can name the functions explicitly, but, again, I'm not trying to fix anything, just wondering why the two syntaxes give different results. I have always considered them functionally interchangeable.
I noticed that both of these forms, and not inside the object, automatically get the names:
function one() {} var two = function() {}
Why is the form object.property = function(){} handled differently than other forms?
javascript function object
Greatbigore
source share