就我而言:

callback instanceof Function

typeof callback == "function"

这有关系吗,有什么区别吗?

Additional Resource:

JavaScript-Garden typeof vs instanceof

推荐答案

Use instanceof for custom types:

var ClassFirst = function () {};
var ClassSecond = function () {};
var instance = new ClassFirst();
typeof instance; // object
typeof instance == 'ClassFirst'; // false
instance instanceof Object; // true
instance instanceof ClassFirst; // true
instance instanceof ClassSecond; // false 

Use typeof for simple built in types:

'example string' instanceof String; // false
typeof 'example string' == 'string'; // true

'example string' instanceof Object; // false
typeof 'example string' == 'object'; // false

true instanceof Boolean; // false
typeof true == 'boolean'; // true

99.99 instanceof Number; // false
typeof 99.99 == 'number'; // true

function() {} instanceof Function; // true
typeof function() {} == 'function'; // true

Use instanceof for complex built in types:

/regularexpression/ instanceof RegExp; // true
typeof /regularexpression/; // object

[] instanceof Array; // true
typeof []; //object

{} instanceof Object; // true
typeof {}; // object

最后一个有点棘手:

typeof null; // object

Javascript相关问答推荐

在JavaScript中对大型级联数组进行切片的最有效方法?

如何使用JavaScript用等效的功能性HTML替换标记URL格式?

有Angular 的material .未应用收件箱中的价值变化

使用AJX发送表单后,$_Post看起来为空

在页面上滚动 timeshift 动垂直滚动条

django无法解析余数:[0] from carray[0]'

将现场录音发送到后端

你怎么看啦啦队的回应?

在Matter.js中添加从点A到另一个约束的约束

如何在Node.js中排除导出的JS文件

如何在 Select 文本时停止Click事件?

如何 for each 输入动态设置输入变更值

为什么可选参数的顺序会导致问题?

<;img>;标记无法呈现图像

Jexl to LowerCase()和Replace()

FindByIdAndUpdate在嵌套对象中创建_id

P5play SecurityError:无法从';窗口';读取命名属性';Add';:阻止具有源的帧访问跨源帧

限制数组中每个元素的长度,

MAT-TREE更多文本边框对齐问题

使用onClick单击子元素时,使用交点观察器的关键帧动画意外运行