Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable is of function type

Tags:

javascript

Suppose I have any variable, which is defined as follows:

var a = function() {/* Statements */}; 

I want a function which checks if the type of the variable is function-like. i.e. :

function foo(v) {if (v is function type?) {/* do something */}}; foo(a); 

How can I check if the variable a is of type Function in the way defined above?

like image 428
Jesufer Vn Avatar asked May 14 '11 05:05

Jesufer Vn


People also ask

How do you know if a variable is typeof?

To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.

How do you check if a variable is a function in Python?

So to be specific you will have to use isinstance(f, (types. FunctionType, types. BuiltinFunctionType)). And of course if you strictly want just functions, not callables nor methods.

Is a function a variable JavaScript?

A function in JavaScript is the set of statements used to perform a specific task. A function can be either a named one or an anonymous one. The set of statements inside a function is executed when the function is invoked or called. A function can be assigned to a variable or passed to a method.

Is function a type in Python?

Syntax of the Python type() functionThe type() function is used to get the type of an object. When a single argument is passed to the type() function, it returns the type of the object. Its value is the same as the object.


1 Answers

if (typeof v === 'function') {     // do something } 
like image 67
selbie Avatar answered Oct 15 '22 20:10

selbie