Quantcast
Channel: Check if a variable is of function type - Stack Overflow
Browsing index pages (23 articles)
↧

Check if a variable is of function type

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...

View Article


Answer by selbie for Check if a variable is of function type

if (typeof v === 'function') { // do something}

View Article


Answer by Paul Rosania for Check if a variable is of function type

Underscore.js uses a more elaborate but highly performant test:_.isFunction = function(obj) { return !!(obj && obj.constructor && obj.call && obj.apply);};See:...

View Article

Answer by Alex Grande for Check if a variable is of function type

Sure underscore's way is more efficient, but the best way to check, when efficiency isn't an issue, is written on underscore's page linked by @Paul Rosania.Inspired by underscore, the final isFunction...

View Article

Answer by dandean for Check if a variable is of function type

@grandecomplex: There's a fair amount of verbosity to your solution. It would be much clearer if written like this:function isFunction(x) { return Object.prototype.toString.call(x) == '[object...

View Article


Answer by user1176126 for Check if a variable is of function type

Try the instanceof operator: it seems that all functions inherit from the Function class:// Test datavar f1 = function () { alert("test"); }var o1 = { Name: "Object_1" };F_est = function () { };var o2...

View Article

Answer by Azmisov for Check if a variable is of function type

I found that when testing native browser functions in IE8, using toString, instanceof, and typeof did not work. Here is a method that works fine in IE8 (as far as I know):function isFn(f){ return !!(f...

View Article

Answer by dalimian for Check if a variable is of function type

There are several ways so I will summarize them allBest way is: function foo(v) {if (v instanceof Function) {/* do something */} };Most performant (no string comparison) and elegant solution - the...

View Article


Answer by Rajesh Paul for Check if a variable is of function type

you should use typeOf operator in js.var a=function(){ alert("fun a");}alert(typeof a);// alerts "function"

View Article


Answer by Cesar Loachamin for Check if a variable is of function type

jQuery (deprecated since version 3.3) Reference$.isFunction(functionName);AngularJSReferenceangular.isFunction(value);LodashReference_.isFunction(value);UnderscoreReference_.isFunction(object); Node.js...

View Article

Answer by GuillaumeL for Check if a variable is of function type

An other simply way:var fn = function () {}if (fn.constructor === Function) { // true} else { // false}

View Article

Answer by moklick for Check if a variable is of function type

Since node v0.11 you can use the standard util function :var util = require('util');util.isFunction('foo');

View Article

Answer by Marcus Junius Brutus for Check if a variable is of function type

The below seems to work for me as well (tested from node.js):var isFunction = function(o) { return Function.prototype.isPrototypeOf(o);};console.log(isFunction(function(){})); //...

View Article


Answer by wsc for Check if a variable is of function type

const foo = function() {};if (typeof foo === 'function') { console.log('is function')}

View Article

Answer by Danny Mor for Check if a variable is of function type

I think you can just define a flag on the Function prototype and check if the instance you want to test inherited thatdefine a flag:Function.prototype.isFunction = true; and then check if it existvar...

View Article


Answer by saeta for Check if a variable is of function type

If you use Lodash you can do it with _.isFunction._.isFunction(function(){});// => true_.isFunction(/abc/);// => false_.isFunction(true);// => false_.isFunction(null);// => falseThis method...

View Article

Answer by Damaged Organic for Check if a variable is of function type

For those who's interested in functional style, or looks for more expressive approach to utilize in meta programming (such as type checking), it could be interesting to see Ramda library to accomplish...

View Article


Answer by Alireza for Check if a variable is of function type

Something with more browser support and also include async functions could be:const isFunction = value => value ? (Object.prototype.toString.call(value) === "[object Function]" || "function" ===...

View Article

Answer by Yassine CHABLI for Check if a variable is of function type

Becareful about this :typeof Object === "function" // true.typeof Array === "function" // true

View Article

Answer by De Bonheur for Check if a variable is of function type

if you are looking for a simple solution: function isFunction(value) { return value instanceof Function}

View Article
Browsing index pages (23 articles)


Latest Images