简体   繁体   中英

Javascript function with undefined parameter and the parameter is defined in function body , is a global variable?

let's suppose we have a javascript function and we want to check if the second parameter is undefined , in that case we want to assign it a value such as

function myFunc(a,b){ 
   if(typeof b==="undefined" ){
      b=0;
   }
   //do stuff
}

Does the variable b in the if block still refer to the function's parameter or a global variable named b is created?

Cheers

Yes, b gets resolved in the lexicalEnvironment (ES5) respectively Activation Object (ES3). It will always get resolved locally, even if a global variable b exists aswell by accessing the pure name.

You would need to explicitly call window.b respectively global.b (node) to access that global variable with the same name.

It refers to the function's parameter. You can test yourself like so:

var b = 45;
function myFunc(a,b){ 
   if(typeof b==="undefined" ){
      b=0;
   }
   alert( b ); //It will output 0
   //do stuff
}

myFunc('');

The answers by @Andre and @Nelson are both correct, but just for clarity's sake:

var c = 123;
var func = (function(a)//a === 1234
{
    var b = c;//local b === global c
    return function(a,c)
    {
        console.log(c === b);//false, unless second argument was 123
        console.log(a === 1234);//false, unless first arg was 1234
        console.log('a masks the a of the outer scope');
    };
})(1234);

JS will first scan the scope of the called function, for any vars used, if it doesn't find them there, it moves on to the outer scope. This can be another function, or, eventually, the global scope. In this example, the inner (returned) function will not be able to access the value of a as defined in the outer function. It's GC'ed when that outer function returned, because it's not being referenced anywhere.

code:

function myFunc(a,b){
   if(typeof b==="undefined" ){
      b=0;
   }
    console.log('b from myFunc:'+b)
}
myFunc(5);
console.log('global:'+b)

output:

b from myFunc:0

b is not defined

Javascript first looks in the local scope for either a local variable or a named function argument. If it finds the symbol there, then that variable is the one that is used.

Only if a matching local symbol is not found is a global symbol used.

Thus, in your example, you will be setting the value of b the function argument.

Once you've defined a local symbol in either a local variable or a named function argument, you can no longer use that name by itself to access a global variable of the same name. If you specifically want to address the global variable, you can do so by prefixing it with window. as in window.b . This specifically targets the global scope so the local scope is not used.

The variable stays on the function if;

  1. the variable is declared inside the function using var or without it
  2. the variable is declared with this as it's parent node the function, example below

    function foo(){ this.bar = 'hello'; }

the variable becomes available in the global scope when

  1. the variable is declared outside any function using var or without it

  2. the variable is declared as it's parent node is window example below

window.bar ='i am global'

YOU NEED DEFINE VARIABLE GLOBALLY :

In the below code t2 is undefined.

    <script>

var t1="";
var t2;
function check(t1,t2){
if (t1===undefined)
{
alert("t1 is undefined");
}
if (t2===undefined)
{
alert("t2 is undefined");
}
}
</script>

REF : http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_undefined

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM