JavaScript 中如何检查函数是否定义
使用 typeof
运算符检查函数是否已定义,例如 typeof myFunction === 'function'
。 typeof
运算符返回一个指示值类型的字符串。 如果函数未定义,则 typeof
运算符返回“undefined”并且不会抛出错误。
if (typeof myFunction === 'function') {
console.log('✅ function is defined');
} else {
// 👇️ this runs
console.log('⛔️ function is NOT defined');
}
我们使用 typeof
运算符来检查函数是否已定义。
该运算符返回一个指示值类型的字符串。 这里有些例子:
console.log(typeof (() => {})); // 👉️ "function"
console.log(typeof function () {}); // 👉️ "function"
console.log(typeof null); // 👉️ "object"
console.log(typeof []); // 👉️ "object"
console.log(typeof {}); // 👉️ "object"
console.log(typeof ''); // 👉️ "string"
console.log(typeof 0); // 👉️ "number"
typeof
运算符在与未声明的变量一起使用时不会抛出错误,而是返回字符串“undefined”。
console.log(typeof doesNotExist); // 👉️ "undefined"
但是,如果在使用 let
或 const
关键字声明变量之前使用 typeof
运算符,则会出现错误。
// ❌ Error: Cannot access 'myFunction' before initialization
if (typeof myFunction === 'function') {
console.log('✅ function is defined');
} else {
console.log('⛔️ function is NOT defined');
}
const myFunction = () => {};
在使用 const
关键字初始化函数之前,我们使用了 typeof 运算符。
这将返回一个错误,因为我们正在尝试访问程序中存在但尚未声明的变量。
如果我们使用 let 关键字声明函数,我们会得到相同的结果。
但是
,如果我们使用var
关键字,则不会因为提升在 JavaScript 中的工作方式而出现错误。
if (typeof myFunction === 'function') {
console.log('✅ function is defined');
} else {
// 👇️ This runs
console.log('⛔️ function is NOT defined');
}
var myFunction = () => {};
我们使用
var
关键字声明函数,因此在使用typeof
运算符时没有出现错误,但是运行了 else 块。
这就是使用 var
关键字时在幕后发生的事情。
var myFunction;
if (typeof myFunction === 'function') {
console.log('✅ function is defined');
} else {
// 👇️ This runs
console.log('⛔️ function is NOT defined');
}
myFunction = () => {};
变量的声明被提升到顶部,但是值的赋值仍然保留在原处。
这就是为什么我们没有收到错误,而且变量还没有函数类型的原因。
我们很少会像这样编写或阅读代码,但是了解这些基本概念是件好事。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
Pandas DataFrame DataFrame.append() 函数
发布时间:2024/04/22 浏览次数:104 分类:Python
-
Pandas 中的 append 方法将两个不同 DataFrame 的行合并,并返回新的 DataFrame。
Pandas DataFrame DataFrame.apply() 函数
发布时间:2024/04/22 浏览次数:188 分类:Python
-
Pandas DataFrame apply()函数将输入的函数应用到 Pandas DataFrame 的每一个沿行或沿列的元素。