8

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.

2
  • 3
    Because the second one is anonymous function hence it has empty string in name property. Commented Dec 11, 2018 at 10:10
  • 2
    @ManishJangir - I don't think that explains it. Both functions here are technically "anonymous", because they're defined with an anonymous function expression: function() {...} - when you could instead have used function my_func() {...} Having just looked at the MDN page for the function name property: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - it seems that for object properties the name does refer to the property name. But this doesn't explain why it doesn't work for y. (And why it is an empty string rather than "anonymous".) Commented Dec 11, 2018 at 10:21

3 Answers 3

0

Since the function in the second case doesn't have any name associated with it so you get empty string.

let x = {
  publish: function() {
    console.log(this.publish.name);
  }
};
let y = {};
y.publish = function() {
  console.log(this.publish.name);
};

let z = {};
z.publish = function hello() {
  console.log(this.publish.name);
};
x.publish();
y.publish();
z.publish();

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain more as I can't find the name in the first case as well.
Well when you use the key value pair the funtion name is same as key.
0

In your second case y.publish, you're assigning a variable identifier to the function but not giving it a name. that would be y.publish = function publish()

4 Comments

I think that's what the OP referred to by "WITHOUT HARDCODING".
The function doesn't have a name
What else is function publish()?
a name, he wrote y.publish = function(){}, that doesn't assign a function a name, it gives it a code identifier. Those are 2 separate things
0

Basically any time an anonymous function expression appears on the right-hand side of something like an assignment or initialization, like:

var boo = function() { /*...*/ };
(or it could be let or const rather than var), or

var obj = {
    boo: function() { /*...*/ }
};

or

doSomething({
    boo: function() { /*...*/ }
});

(those last two are really the same thing), the resulting function will have a name (boo, in the examples).

There's an important, and intentional, exception: Assigning to a property on an existing object:

obj.boo = function() { /*...*/ }; // <== Does not get a name

Search for SetFunctionName on the following:

1. Property initializer semantics

2. Assignment Operator Semantics

3 Comments

Is this behaviour documented anywhere? I can't find it on MDN.
Updated my answer
Thanks, although I'm not able at the moment to wade through the formal specification to find this. But I did look at the question mentioned that this one is apparently a duplicate of, and that gives some interesting context.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.