Succinct response:
This is the "normal" behavior in Javascript. The language has advanced and today it is already possible to use const
to declare constants that cannot be superscripted.
Elongated response:
From the beginning Javascript allows you to overwrite variable and function names, and even assign values to variables without declaring them, "declaring them" in global scope, which has generated bugs difficult to detect.
In modern versions of Javascript much of this has been fixed and for your case the fix is to use const
, by assigning a function to a variable.
Thus in Ecmascript 2015 (version 6 of Javascript) exists const
that starts a variable that can no longer be over-written without generating build error.
Your questions:
- This is really the expected behavior when we declare 2
methods with the same name and parameters in js, it will always
override and will prevail the last declared?
Yeah. Unless you use const
(which will generate an error if the names are equal). This is the normal behavior, equal to CSS, the last to be declared.
- Is there any explanation for this behavior, if yes what it is
explanation?
That’s how the language was built, kind of flexible... But it’s being reinforced/corrected in the new versions.
- Is there any way to do it that causes some mistake or something
if someone makes this mistake and declares 2 methods with the same
name and parameters?
Yes, using const
the problem is fixed. That is, it gives error if a variable is over-written. In this case you can use:
const teste = function(){
console.log('teste 1');
}
and if you do it then const teste = outra coisa...
compiler gives error and does not run.
- In case 3 has any way of doing, what to do if the functions
are in different js files?
Then you have to encapsulate things. Either you use CJS, AMD or ES6 modules. There is not yet a universal specification on the part of language, but there are proposals that are under consideration in this regard. If you use the Babel compiler you can use these modules now.
So there are two answers:
With compiled modules you can declare everything in the "global" scope because the module does not share this scope with other files. All variables are encapsulated and the only thing you can export out of the module is via the reserved word exports
. An example:
function teste(){
console.log(1);
}
module.exports = teste;
in that way teste
is not visible to other files.
Using native Javascript, where all files share the global space:
Here you have to encapsulate things yourself. The concept is identical but you have to use Iifes to hide variables from each other.
For example:
// ficheiro A
(function(global) {
function teste() {
console.log('teste A');
}
global.testeA = teste;
})(this);
// ficheiro B
(function(global) {
function teste(nr) {
var res = nr * nr;
console.log('teste B', res);
}
global.testeB = teste;
})(this);
testeA();
testeB(17);
This doubt must have arisen because you are used to typed programming, in which there can be "equal" methods with different parameters.
– Guilherme Lautert
Related link: Enabling Javascript method Overload
– novic
@Virgilionovic could be my mistake,
function adiciona(produto) {
 console.log(produto);
}
function adiciona(produto, categoria) {
 console.log(produto, categoria);
}

adiciona(1);
adiciona(1, 2);
did not work, at least the article hinted that it works :/ (tested in Chrome)– Guilherme Nascimento
Opa @Guilhermenascimento I was curious not know this issue in Javascript that is not my strong, but, boy I did not really test the validity of this link, I will test it and maybe some charitable soul can guide us on this ... ! Any news I tell you ... !!!
– novic
@Guilhermenascimento The link actually has some coding errors, but by the logic I saw this really ok, the problem is that it would be related to methods of a generated function, I believe that it cannot be applied in
window
– Guilherme Lautert
@Guilhermelautert the test I took was like this
(function () {
 function adiciona(produto) {
 console.log("função 1", produto);
 }
 function adiciona(produto, categoria) {
 console.log("função 2", produto, categoria);
 }

 adiciona(1);
 adiciona(1, 2);
 })();
, and yet the only result is"função 2"
.– Guilherme Nascimento
@Guilhermelautert this from Users seems to me the second example, maybe it is fault in the author’s writing and he wants to say that it did not work, I mean the text seems to me confused
– Guilherme Nascimento
@Guillhermenascimento see
– Guilherme Lautert
@Guilhermelautert this I came to test yes, what I’m really talking about is how the text was written, it led to a misunderstanding of the situation in the first part, at least to me it seemed this "In Javascript, define a function with the same name to overwrite:" =)
– Guilherme Nascimento