Can someone explain what happens in the below scenario:(this was an interview question)
function test(a){
alert(a);
}
function test(a,b){
alert(a,b);
}
function callTest(){
test(a);
test(a,b);
}
I was asked how the javascript calls the methods, and what happens behind the scenes in this scenario
If you run it in that order, test(a) will be replaced by test(a,b)
When callTest() is run, then test(a) will alert(a, undefined), then test(a,b) will alert(a,b)
function test(a){ // f1. never called.
alert(a);
}
function test(a,b){ // f2. overwrite f1 function because name of two functions is same.
alert(a,b);
}
function callTest(){
test(1); // called f2 with (1, undefined)
test(1,2); // called f2 with (1, 2)
}
callTest();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.