Javascript : 函数参数未定义
Javascript : Function parameter is undefined
对于变量 x,设 typeof x === 'undefined'
我写了一个函数来检查变量是否为 undefined
或
var isUndefined = function(obj){
return typeof obj == 'undefined';
};
这里有两种情况:
console.log(typeof x === 'undefined') // returns true
isUndefined(x) // throws this error ReferenceError: x is not defined
为什么我不能在这里传递未定义的变量?
基本上undefined代表的是value,没有被用户正式定义。考虑你的例子:
var isUndefined = function(obj){
return typeof obj == 'undefined';
};
因此,isUndefined()
当不带任何参数调用时,obj 的值将在函数中未定义。
看到这个 post。您必须了解未定义和未声明变量之间的区别。
至少你会这样做:
var x; //undefined until assignment;
isUndefined(x); // returns true;
变量 x
未声明,但您在全局上下文中询问 typeof
typeof x === 'undefined' // Returns true
相当于下面
typeof window.x === 'undefined' //True
但是下面的代码
x === undefined //Throws error
和
x in window //Throws Error
因为永远不会在全局范围内使用任何标准 JavaScript 数据类型设置或声明变量。
阅读 MDN 博客,其中指出:
JavaScript is a statically scoped language, so knowing if a variable is declared can be read by seeing whether it is declared in an enclosing context. The only exception is the global scope, but the global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object
现在,当调用 isUndefined
函数时,您将传递变量 x
的引用,该变量未在父作用域中声明,并在功能作用域中运行代码,试图获取对x
.
由于 x
没有声明,因此 javascript 解释器在运行时没有任何意义或值要传递给 isUndefined
。因此抛出 引用错误 要求开发人员至少声明变量,当未分配任何值时将默认设置为原始类型 undefined
.
现在通过将 x 设置为原始类型 undefined 明确如下
var x; // Equivalent to window.x
或
var x = undefined; //Equivalent to window.x = undefined
现在 JavaScript 解释器知道在全局范围 window
中声明了一个名为 x
的变量并设置为原始数据类型。下面的语句变成 true
:
typeof x === 'undefined' // Returns true
x === undefined //Returns true
x in window // Returns true
isUndefined(x) // Returns true
对于变量 x,设 typeof x === 'undefined'
我写了一个函数来检查变量是否为 undefined
或
var isUndefined = function(obj){
return typeof obj == 'undefined';
};
这里有两种情况:
console.log(typeof x === 'undefined') // returns true
isUndefined(x) // throws this error ReferenceError: x is not defined
为什么我不能在这里传递未定义的变量?
基本上undefined代表的是value,没有被用户正式定义。考虑你的例子:
var isUndefined = function(obj){
return typeof obj == 'undefined';
};
因此,isUndefined()
当不带任何参数调用时,obj 的值将在函数中未定义。
看到这个 post。您必须了解未定义和未声明变量之间的区别。
至少你会这样做:
var x; //undefined until assignment;
isUndefined(x); // returns true;
变量 x
未声明,但您在全局上下文中询问 typeof
typeof x === 'undefined' // Returns true
相当于下面
typeof window.x === 'undefined' //True
但是下面的代码
x === undefined //Throws error
和
x in window //Throws Error
因为永远不会在全局范围内使用任何标准 JavaScript 数据类型设置或声明变量。
阅读 MDN 博客,其中指出:
JavaScript is a statically scoped language, so knowing if a variable is declared can be read by seeing whether it is declared in an enclosing context. The only exception is the global scope, but the global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object
现在,当调用 isUndefined
函数时,您将传递变量 x
的引用,该变量未在父作用域中声明,并在功能作用域中运行代码,试图获取对x
.
由于 x
没有声明,因此 javascript 解释器在运行时没有任何意义或值要传递给 isUndefined
。因此抛出 引用错误 要求开发人员至少声明变量,当未分配任何值时将默认设置为原始类型 undefined
.
现在通过将 x 设置为原始类型 undefined 明确如下
var x; // Equivalent to window.x
或
var x = undefined; //Equivalent to window.x = undefined
现在 JavaScript 解释器知道在全局范围 window
中声明了一个名为 x
的变量并设置为原始数据类型。下面的语句变成 true
:
typeof x === 'undefined' // Returns true
x === undefined //Returns true
x in window // Returns true
isUndefined(x) // Returns true