如何知道函数是类还是函数 [英] How to know if a function is a class or a function
问题描述
我实际上是想区分(在我的代码中)函数和类。
I'm actually trying to differentiate (in my code) functions and classes.
我需要知道,在一个简单的console.log中,如果参数为我的函数是一个类或一个简单的函数。
I need to know, in a simple console.log, if the parameter of my function is a class or a simple function.
我实际上是关于类,而不是对象,但实际上是一个类。
I'm actually taking about class, not object, but really a class.
喜欢:
function(item){
if(isClass(item)){
console.log('it s a class !');
}else{
if(isFunc(item)){
console.log('it s a function !');
}
}
}
编辑:angular 2能够区分注入器容器类中的函数和类
Edit : angular 2 is able to distinguish a function and a class in his injector container class
推荐答案
JavaScript中没有任何类。虽然函数可以使用 new
关键字进行实例化。
There aren't any classes in JavaScript. Though functions can be instanciated with the new
keyword.
您可以使用 instanceof
检查某些内容是否为函数:
You can check if something is a function with instanceof
:
var a = function() {};
a instanceof Function; // true
instanciated函数将是该特定函数的一个实例:
An instanciated function will be an instance of that specific function:
var b = new a();
b instanceof Function; // false
b instanceof a; // true
这篇关于如何知道函数是类还是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!