---
layout: page
title: "JavaScript is_callable function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/is_callable:439
- /functions/view/is_callable
- /functions/view/439
- /functions/is_callable:439
- /functions/439
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's is_callable

{% codeblock var/is_callable.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/is_callable.js raw on github %}
function is_callable (v, syntax_only, callable_name) {
  // From: http://phpjs.org/functions
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // +      input by: François
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // %        note 1: The variable callable_name cannot work as a string variable passed by reference as in PHP (since JavaScript does not support passing strings by reference), but instead will take the name of a global variable and set that instead
  // %        note 2: When used on an object, depends on a constructor property being kept on the object prototype
  // *          test: skip
  // *     example 1: is_callable('is_callable');
  // *     returns 1: true
  // *     example 2: is_callable('bogusFunction', true);
  // *     returns 2: true // gives true because does not do strict checking
  // *     example 3: function SomeClass () {}
  // *     example 3: SomeClass.prototype.someMethod = function (){};
  // *     example 3: var testObj = new SomeClass();
  // *     example 3: is_callable([testObj, 'someMethod'], true, 'myVar');
  // *     example 3: myVar;
  // *     returns 3: 'SomeClass::someMethod'
  // *     example 4: is_callable(function () {});
  // *     returns 4: true

  var name = '',
    obj = {},
    method = '';
  var getFuncName = function (fn) {
    var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
    if (!name) {
      return '(Anonymous)';
    }
    return name[1];
  };
  if (typeof v === 'string') {
    obj = this.window;
    method = v;
    name = v;
  }
  else if (typeof v === 'function') {
    return true;
  }
  else if (Object.prototype.toString.call(v) === '[object Array]' &&
        v.length === 2 && typeof v[0] === 'object' && typeof v[1] === 'string') {
    obj = v[0];
    method = v[1];
    name = (obj.constructor && getFuncName(obj.constructor)) + '::' + method;
  }
  else {
    return false;
  }
  if (syntax_only || typeof obj[method] === 'function') {
    if (callable_name) {
      this.window[callable_name] = name;
    }
    return true;
  }
  return false;
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/is_callable.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/is_callable.js)

### Example 1
This code
{% codeblock lang:js example %}
is_callable('is_callable');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
is_callable('bogusFunction', true);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
true // gives true because does not do strict checking
{% endcodeblock %}

### Example 3
This code
{% codeblock lang:js example %}
function SomeClass () {}
SomeClass.prototype.someMethod = function (){};
var testObj = new SomeClass();
is_callable([testObj, 'someMethod'], true, 'myVar');
myVar;
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'SomeClass::someMethod'
{% endcodeblock %}


### Other PHP functions in the var extension
{% render_partial _includes/custom/var.html %}
