Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I search on google but did not find clear answer about anonymous and named function. I am looking for simple answer which one is better any why or its depends on requirement. So I am looking forward to your valuable answer about these topic. Your answer is really help me out to understand this. Thanks in advance
The advantages of a named function (expression) are:
makes it more reliable to call the function recursively, since the name becomes a binding inside the function itself.
can create a better call stack (by using the function name instead of <anonymous>
Using a named function (expression) might not be possible if
you care about IE6, which doesn't handle them properly (it creates two functions)
you can't think of a name that wouldn't shadow a variable you need to access inside the function
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
function exampleFunction() {}
Why do you need the () after defining exampleFunction??
The brackets at the end of a function can be used to define parameters when calling it.
For example:
alertSentence("Hello, this is an example!");
function alertSentence(string) {
alert(string);
}
This would show an alert dialog with the string: "Hello, this is an example!"
Javascript is a language that was defined following the patterns of c. Wikipedia says it's part of the simula programming language family link. Languages in this family define function parameters using parentheses. Other language families have other ways (see python for an example). Ultimately it's a bit of arbitrary syntax that allows programmers to explicitly define what they want the computer to do and a way for the computer to understand them.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In Javascript, is it acceptable to rename built in functions. For example, in my code, I use document.querySelector() tons of times. This is extremely long and tedious to type every single time (even with autocomplete from the IDE's side). Therefore, I decided to create a new function with a shorter name like the following:
let qs = selector => document.querySelector(selector);
Is this an acceptable practice in the JS community? Is this considered bad code? If so, why? Additionally, if doing this is alright, what are some drawbacks?
Thanks.
No.
Someone is going to come behind you to edit your code.
They will then have to track down your renaming function to actually see what it does.
Create an snippet in your IDE if it’s that much of an issue for you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hi I am novice to Angularjs, I am having lot of factories in my application, the scenario is lets assume like obj1 in factoryA. At some point of time when I console.log(obj1) , then it shows multiple properties inside it. It is getting updated from other factories and controllers. Since it is big application I dont know the complete flow and it is very hard to find out. So, is there any way to track down history like from where and all the particular object in factory is invoked and updated ?.
Use the debugger keyword as breakpoints.
You shall be able to examine all values that are in the current scope.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
What should I take into consideration when deciding how to pass callbacks to addEventListener, given the following approaches?
element.addEventListener('click', function(){
// ...
});
or
function doSomething(){
// ...
}
element.addEventListener('click', doSomething);
or
element.addEventListener('click', function doSomething(){
// ...
});
Your first code snippet uses an anonymous function. Todd Motto has written an article listing some reasons why you should avoid using anonymous functions in callback functions:
Are more difficult to debug
Cannot be reused
Cannot be tested easily
Do not describe the role of the function
Make code lack structure
Create messier/unclear code
Documentation will suffer (things like jsDoc)
The first way is called an anonymous function. I wouldn't really say there's a way you 'have to' or 'should' do it, but you'll usually see them used to create really simple one-off bits of logic that you don't want to bloat your code writing a new block for.
If you will need to debug your code, or you might want to reuse the code in the function, obviously your life will be better if you write a named function.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
When binding event handlers, I've found the need to create a function, since it would need to be referenced twice; once initially, and once to the event binding:
// Define function
function doSomething(){...}
// Call function initially
doSomething();
// Pass function as the event handler
$("#theElement").on("click", doSomething);
But then I realized I could start doing this by passing a self-invoking anonymous function as the event handler and returning arguments.callee:
// Much cleaner!
$("#theElement").on("click", (function(){
...
return arguments.callee;
})());
Assuming that I do not ever use this function outside of these two instances, is it an okay practice to do so?
Well, most people would just stick with the first block, because it is clear, simple, and idiomatic. The second block saves one line of code, but the cost is code that does something strange. If your code does something strange then a lot of readers are likely to misunderstand its behavior and any changes they make will be likely to introduce defects.