Javascript:函数参数未定义

Ama*_*pta 5 javascript undefined

对于变量 x,令typeof x === 'undefined'

我写了一个函数来检查变量是否undefined存在

var isUndefined = function(obj){
  return typeof obj == 'undefined';
};
Run Code Online (Sandbox Code Playgroud)

这里有两种情况:

  1. console.log(typeof x === 'undefined') // returns true
  2. isUndefined(x) // throws this error ReferenceError: x is not defined

为什么我不能在这里传递未定义的变量?

小智 4

请参阅此帖子。您必须了解未定义变量和未声明变量之间的区别。

至少你会这样做:

var x; //undefined until assignment;
isUndefined(x); // returns true;
Run Code Online (Sandbox Code Playgroud)