Why is the output 1?

topic description

sources of topics and their own ideas

related codes

/ / Please paste the code text below (do not replace the code with pictures)

var obj = {a:1};
function fun (obj){
    obj = {a:2}
}
fun(obj)
console.log(obj)

what result do you expect? What is the error message actually seen?

Why is the output {avl 1}? Shouldn"t the obj in the
function be promoted as a global variable without a var declaration?

Oct.15,2021

var obj = {a:1};
function fun (obj){
    console.log(arguments);
    obj = {a:2}
}
fun(obj)
console.log(obj)

try this and you'll see


  

the expression obj is a formal parameter, which is valid only within the function and does not affect the obj outside the function

.
Menu