迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中如何检查函数是否定义

作者:迹忆客 最近更新:2022/12/15 浏览次数:

使用 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');
}

JavaScript 中如何检查函数是否定义

我们使用 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"

但是,如果在使用 letconst 关键字声明变量之前使用 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 = () => {};

JavaScript 中如何检查函数是否定义错误代码

在使用 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 = () => {};

变量的声明被提升到顶部,但是值的赋值仍然保留在原处。

这就是为什么我们没有收到错误,而且变量还没有函数类型的原因。

我们很少会像这样编写或阅读代码,但是了解这些基本概念是件好事。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便