Suppose I have an 2 objects x and y. The details are written in the code below.
let x = {
publish: function() {
console.log(this.publish.name);
}
};
let y = {};
y.publish = function() {
console.log(this.publish.name);
};
x.publish();
y.publish();
I was getting difference in the outputs calling x.publish() and y.publish().
The former returned the name of the function while the latter returned empty. Can anyone explain why is this happening, and is there any other
possible way I can retrieve the function name in latter(WITHOUT HARDCODING). I am using NodeJs version 8.
function() {...}- when you could instead have usedfunction my_func() {...}Having just looked at the MDN page for the functionnameproperty: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - it seems that for object properties thenamedoes refer to the property name. But this doesn't explain why it doesn't work fory. (And why it is an empty string rather than"anonymous".)