在我的特殊情况下:

callback instanceof Function

or

typeof callback == "function"

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

额外的资源:

花园typeof vs instanceof


当前回答

根据MDN关于typeof的文档,用"new"关键字实例化的对象类型为'object':

typeof 'bla' === 'string';

// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean(true) === 'object'; 
typeof new Number(1) === 'object'; 
typeof new String('abc') === 'object';

而关于instanceof点的文档说明:

const objectString = new String('String created with constructor');
objectString instanceOf String; // returns true
objectString instanceOf Object; // returns true

因此,如果有人想检查,例如,某东西是一个字符串,无论它是如何创建的,最安全的方法是使用instanceof。

其他回答

我从小接受严格的面向对象教育

callback instanceof Function

字符串容易出现我糟糕的拼写或其他拼写错误。而且我觉得读起来更好。

为了把事情弄清楚,你需要知道两个事实:

instanceof操作符测试构造函数的prototype属性是否出现在对象的prototype链中的任何位置。在大多数情况下,这意味着对象是通过使用该构造函数或其后代构造函数创建的。但prototype也可以通过Object.setPrototypeOf()方法(ECMAScript 2015)或__proto__属性显式设置(旧浏览器,已弃用)。但是,由于性能问题,不建议更改对象的原型。

因此,instanceof仅适用于对象。在大多数情况下,你不会使用构造函数来创建字符串或数字。你可以。但你几乎从来没有这样做过。

instanceof也不能检查使用哪个构造函数创建对象,但返回true,即使对象是从被检查的类派生的。在大多数情况下,这是理想的行为,但有时并非如此。所以你要保持这种心态。

另一个问题是不同的作用域具有不同的执行环境。这意味着它们有不同的内置(不同的全局对象,不同的构造函数,等等)。这可能会导致意想不到的结果。

例如,[]instanceof window.frames[0].Array将返回false,因为Array。== window.frames[0]. array和数组继承自前者。 此外,它不能用于未定义的值,因为它没有原型。

The typeof operator tests whether value belong to one of six basic types: "number", "string", "boolean", "object", "function" or "undefined". Where the string "object" belong all objects (except functions, which are objects, but have its own value in typeof operator), and also "null" value and arrays (for "null" it's a bug, but this bug is so old, so it's become a standard). It doesn't rely on constructors and can be used even if value is undefined. But it's doesn't give any details about objects. So if you needed it, go to instanceof.

现在我们来讨论一个棘手的问题。如果使用构造函数创建基元类型呢?

let num = new Number(5);
console.log(num instanceof Number); // print true
console.log(typeof num); // print object
num++; //num is object right now but still can be handled as number
//and after that:
console.log(num instanceof Number); // print false
console.log(typeof num); // print number

就像魔法一样。但事实并非如此。这就是所谓的装箱(按对象包装原语值)和解装箱(从对象中提取包装的原语值)。这样的代码似乎“有点”脆弱。当然,您可以避免使用构造函数创建原始类型。但还有另一种可能的情况,拳击可能会打到你。在原语类型上使用Function.call()或Function.apply()时。

function test(){
  console.log(typeof this);
} 
test.apply(5);

为了避免这种情况,你可以使用严格模式:

function test(){
  'use strict';
  console.log(typeof this);
} 
test.apply(5);

乌利希期刊指南: 自ECMAScript 2015以来,又增加了一种名为Symbol的类型,它有自己的typeof == " Symbol "。

console.log(typeof Symbol());
// expected output: "symbol"

你可以在MDN上阅读它:(Symbol, typeof)。

还有一种情况是你只能用instanceof进行排序——它返回true或false。使用typeof你可以得到typeof提供的东西

我在Safari 5和Internet Explorer 9中发现了一些非常有趣的(读作“可怕的”)行为。我在Chrome和Firefox中成功地使用了这个功能。

if (typeof this === 'string') {
    doStuffWith(this);
}

然后我在IE9中测试,它根本不能工作。大惊喜。但在Safari中,它是间歇性的!我开始调试,发现ie总是返回false。但最奇怪的是,Safari似乎在它的JavaScript VM中做了某种优化,第一次为真,但每次点击重载时为假!

我的大脑几乎爆炸了。

所以现在我决定这样做:

if (this instanceof String || typeof this === 'string')
    doStuffWith(this.toString());
}

现在一切都很好。请注意,你可以调用"a string". tostring(),它只是返回字符串的副本,即。

"a string".toString() === new String("a string").toString(); // true

所以从现在开始我将两者都用。

当然这很重要........!

让我们用例子来解释一下。在我们的例子中,我们将以两种不同的方式声明函数。

我们将同时使用函数声明和函数构造函数。我们将了解typeof和instanceof在这两种不同场景中的行为。

使用函数声明创建函数:

function MyFunc(){  }

typeof Myfunc == 'function' // true

MyFunc instanceof Function // false

对于这种不同的结果,可能的解释是,因为我们做了一个函数声明,typeof可以理解它是一个函数。因为typeof检查typeof的表达式是否被操作,在我们的例子中,MyFunc是否实现了Call Method。如果它实现了Call方法,它就是一个函数。为了澄清,请检查ecmascript的typeof规范。

使用函数构造函数创建函数:

var MyFunc2 = new Function('a','b','return a+b') // A function constructor is used 

typeof MyFunc2 == 'function' // true

MyFunc2 instanceof Function // true

这里typeof断言MyFunc2是一个函数,也是一个instanceof操作符。我们已经知道typeof检查MyFunc2是否实现了Call方法。由于MyFunc2是一个函数,它实现了调用方法,这就是typeof如何知道它是一个函数。另一方面,我们使用函数构造函数来创建MyFunc2,它成为函数构造函数的一个实例。这就是为什么instanceof也解析为true。

用什么更安全?

我们可以看到,在这两种情况下,typeof操作符都可以成功地断言我们在处理一个函数,它比instanceof更安全。instanceof在函数声明的情况下将失败,因为函数声明不是function构造函数的实例。

最佳实践:

正如Gary Rafferty所建议的,最好的方法应该同时使用typeof和instanceof。

  function isFunction(functionItem) {

        return typeof(functionItem) == 'function' || functionItem instanceof Function;

  }

  isFunction(MyFunc) // invoke it by passing our test function as parameter