Just wanted to ask: Is there some reason to name a function as variable in JavaScript?
(I mean using Dep
two times. Just was crawling in Vue.js and found this)
var Dep = function Dep () {
this.id = uid++;
this.subs = [];
};
Just wanted to ask: Is there some reason to name a function as variable in JavaScript?
(I mean using Dep
two times. Just was crawling in Vue.js and found this)
var Dep = function Dep () {
this.id = uid++;
this.subs = [];
};
The variable name is not the name of the function; it's just a variable that happens to contain a reference to the function.
The "Dep" after the function
keyword is the name of the function. No matter what happens later to the variable called "Dep", inside the function "Dep" will always be its own name.
In this case of course it doesn't matter at all because the function does not refer to itself.
Back a while ago it was a good idea to do that to avoid having errors reported in the browser console as being from an "anonymous function". Modern browser debugging environments are pretty good at grabbing an appropriate name, but there are still cases when they can't.
Is there some reason > I mean using Dep two times
If you mean why not ->
var Dep = function () {
this.id = uid++;
this.subs = [];
};
IOW: Why not just assign an anonymous function, It's most likely for debugging reasons, you'll get better named call-stack's if you name your functions.