Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 判断是否是函数

作者:影之哀伤   发布日期:2026-08-06   浏览:99

// 判断是否是函数的示例代码

function isFunction(func) {
  // 使用 typeof 操作符来判断传入的参数是否为函数
  return typeof func === 'function';
}

// 示例用法
console.log(isFunction(function() {})); // true
console.log(isFunction(() => {}));      // true
console.log(isFunction(123));           // false
console.log(isFunction("string"));      // false
console.log(isFunction({}));            // false
console.log(isFunction(null));          // false

解释说明:

  • typeof 是一个操作符,用于返回操作数的数据类型。
  • 当操作数是一个函数时,typeof 返回字符串 'function'
  • 因此,通过检查 typeof func === 'function' 可以判断传入的参数是否为函数。

上一篇:js 判断元素是否存在

下一篇:js 对象是否包含某个属性

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js fill方法

js 数组连接

js json数组

js 数组复制

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站