Parameters between text [closed] - javascript

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 last month.
Improve this question
Why isn't there a way to have functions with the parameters in between text? For example:
function _(param1)plus(param2){
return param1+param2;
}
This is just an idea I had on my mind and I didn't try anything. However, if I did this I'd get an error

I don't really understand the question, but you can do this instead:
function plus(param1, param2){
return param1 + param2
}
Hope this helps!

Because each programming language has its rules (syntax enforced by the parser). And a common rule for almost all languages (the same for JavaScript) is that functions can have parameters, and you specify these parameters in the brackets ().
So you can not continue to build function's name after the brackets, because the JavaScript will assume that everything before the brackets is the name of the function.

Why would you want this ? If you need to add text for some reason, that's what comments are for.
Having text between parameters will make code hard to read and maintain. It could even be difficult to determine what is a parameter or not. The compiler / interpreter will certainly need more time and resources to determine what's a parameter and what is just text.

Related

Finding file name with variables in between [closed]

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 3 months ago.
Improve this question
Having a hard time with Regex.
What would be the regex for finding a file name with variable in between them?
For eg:
File name : DON_2010_JOE_1222022.txt
In the above file name the words DON, JOE and the format .txt will remain constant. Rest numbers might change for every file. There could be characters as well instead of numbers in those two places.
What im looking for is basically something like DON_*_JOE_*.txt with * being whatever it could be.
Can someone please help me with this?
I tried DON_*_JOE_*.txt and obviously it did not work.
DON_(?<firstString>.*)_JOE_(?<secondString>.*).txt
You can use this. To access the specific group, you can use matcher.group("firstString").
In JavaScript:
"DON_2010_JOE_1222022.txt".match(/DON_.+_JOE_.+\.txt/)
whatever it could be
It is .+ except new lines.

Pretty Basic question but why do you need brackets after defining a function? [closed]

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.

Script src with 'arguments' [closed]

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 7 years ago.
Improve this question
I often see that 'src' property in script tag has 'arguments/parameters' (I am sorry if I am not using the proper terminology, this is why I am asking). I would like to know:
What is the proper name?
Why is this done this way?
Where is it used?
Is there any advantage by using/doing it this way?
Here it is an example.
<script type='text/javascript' src='root/somedir.js?arg=somevalue'></script>
Basically, it depends of context. And yes, it's called query string.
In some cases it might be used as cache buster (e.g. ?timestamp={current_timestamp}) in order to avoid client-side caching of specified resource.
In other cases it might be used to generate script based on any condition (for example: api key, custom parameter, etc).
Assume you make call to js weather widget, and the documentation says that you should pass the city name as parameter to get the right data, e.g.
<script src="//some-weather-widget.js?city=Sofia"></script>
It's not perfect example, but I think you got the idea.

Is it bad form to treat argument names the same as variable names in javascript? [closed]

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
Whenever I define a function in javascript, I frequently name the place holder what I would like them to be. This leads to the problem that, declaring those arguments as variables in the function makes them have the same name. Is this a problem (because I do not often see it done).
For example:
var words = 'Hello there';
var talk = function talk (words) {
// Realizing this does nothing, I am leaving it in because other answers refer to it.
var words = words;
//I suppose this could also be this.words = words?
console.log(words);
}
talk(words); //To log "hello there"
This is an extreme example, but in many instances it seems like this would make more sense than using similar and not quite correct words (like var letters = words)
As a principle for readability, it is best to avoid unnecessary confusion. In your example, if your var-words was a deep copy of words that was then modified, then what would get printed? Even if you know the answer, will your fellow developers? Will they actively go and find out the correct answer before modifying the code? Especially in the case of javascript where it tries to make all code as runnable as possible, it could lead to undefined behaviour and bugs.
In most scenarios this wouldn't need to happen because you should be naming your copies appropriately, or not having them at all if they don't serve a purpose that deserves a new name (such as sortedWords, or duplicateWords, fiveLetterWords, etc)
If you HAVE to do as your extreme example states, the best I can come up with is changing the argument variable name to "words_argument" or something similar to avoid confusion.
Edit: seems like OP edited the question to remove the 'var words = words' line within the function, which is what most of my answer was referring to
You should avoid variable hoisting in JS because this could lead to a bigger problems
refer to these links:
http://thecomputersarewinning.com/post/a-dangerous-example-of-javascript-hoisting/
http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

Anonymous function vs Named function which is better and why [closed]

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

Categories