Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Last active April 16, 2017 05:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericelliott/7e3f2beecf0ec52a56b3 to your computer and use it in GitHub Desktop.
Save ericelliott/7e3f2beecf0ec52a56b3 to your computer and use it in GitHub Desktop.
Secret mouse!
let animal = {
animalType: 'animal',
describe () {
return `An ${this.animalType} with ${this.furColor} fur,
${this.legs} legs, and a ${this.tail} tail.`;
}
};
let mouseFactory = function mouseFactory () {
let secret = 'secret agent';
return Object.assign(Object.create(animal), {
animalType: 'mouse',
furColor: 'brown',
legs: 4,
tail: 'long, skinny',
profession () {
return secret;
}
});
};
let james = mouseFactory();
@pruett
Copy link

pruett commented Apr 25, 2015

i'm curious about line 10:
let mouseFactory = function mouseFactory () {

Are there any benefits in naming the anonymous function? What would be the difference in writing it like this?
let mouseFactory = function () {

And to take it further with ES6 syntax...
let mouseFactory = () => {

Just curious if there are any benefits that I'm not aware of.

@ericelliott
Copy link
Author

It's not anonymous if it has a name. The primary benefit is that the name will appear in debugging callstacks.

@ericelliott
Copy link
Author

The proper terminology for this is "named function expression".

@pruett
Copy link

pruett commented Apr 29, 2015

cool, thanks, makes sense. i've seen this in the past and have always wondered.

@IShotTheSheriff
Copy link

There is one more thing I would like to clarify. Why you used named function expression instead of function declaration in this particular case? Is it just your preferred style or there is something more to it?

Only difference between two approches I could recognise is that named function expression won't be hoisted so you can't use it before line 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment