Use PHP functions in JavaScript

JavaScript function_exists

Checks if the function exists

1
2
3
4
56
7
8
9
1011
12
13
14
1516
function function_exists (func_name) {
    // Checks if the function exists  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/function_exists    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Steve Clay
    // +   improved by: Legaev Andrey
        // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: function_exists('isFinite');    // *     returns 1: true
    if (typeof func_name === 'string') {
        func_name = this.window[func_name];
    }
    return typeof func_name === 'function';}
external links: original PHP docs | raw js source

Examples

Running

1
function_exists('isFinite');

Should return

1
true

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have function_exists goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Arignmor
9 Nov '11 Permalink

q  

Gravatar
Otherne1981
11 Sep '11 Permalink

q  

Gravatar
Merfits1982
11 Sep '11 Permalink

q  

Gravatar
zolkivy1992
11 Sep '11 Permalink

q  

Gravatar
greaste1980
11 Sep '11 Permalink

q  

Gravatar
soonync1982
11 Sep '11 Permalink

q  

Gravatar
niduame1990
11 Sep '11 Permalink

q  

Gravatar
Agreern1991
11 Sep '11 Permalink

q  

Gravatar
pourrex1981
11 Sep '11 Permalink

q  

Gravatar
entaila1987
11 Sep '11 Permalink

q  

Gravatar
Otherne1981
11 Sep '11 Permalink

q  

Gravatar
Merfits1982
11 Sep '11 Permalink

q  

Gravatar
greaste1980
10 Sep '11 Permalink

q  

Gravatar
greaste1980
10 Sep '11 Permalink

q  

Gravatar
soonync1982
10 Sep '11 Permalink

q  

Gravatar
niduame1990
10 Sep '11 Permalink

q  

Gravatar
Agreern1991
10 Sep '11 Permalink

q  

Gravatar
pourrex1981
10 Sep '11 Permalink

q  

Gravatar
entaila1987
10 Sep '11 Permalink

q  

Gravatar
Otherne1981
10 Sep '11 Permalink

q  

Gravatar
Otherne1981
10 Sep '11 Permalink

q  

Gravatar
Merfits1982
9 Sep '11 Permalink

q  

Gravatar
zolkivy1992
9 Sep '11 Permalink

q  

Gravatar
soonync1982
9 Sep '11 Permalink

q  

Gravatar
niduame1990
9 Sep '11 Permalink

q  

Gravatar
Agreern1991
9 Sep '11 Permalink

q  

Gravatar
pourrex1981
9 Sep '11 Permalink

q  

Gravatar
entaila1987
9 Sep '11 Permalink

q  

Gravatar
Merfits1982
9 Sep '11 Permalink

q  

Gravatar
zolkivy1992
9 Sep '11 Permalink

q  

Gravatar
greaste1980
9 Sep '11 Permalink

q  

Gravatar
soonync1982
9 Sep '11 Permalink

q  

Gravatar
niduame1990
9 Sep '11 Permalink

q  

Gravatar
Agreern1991
9 Sep '11 Permalink

q  

Gravatar
pourrex1981
9 Sep '11 Permalink

q  

Gravatar
entaila1987
9 Sep '11 Permalink

q  

Gravatar
Brett Zamir
6 Aug '11 Permalink

q  I assume the compiler might be looking for the text "function_name", so I changed that variable name in the function as a workaround. See if that works...

Gravatar
Bug in function_exists
5 Aug '11 Permalink

q  Hi guys

Firstly thanks for a great little script!
I am a php programmer and having phpjs is great!

I downloaded the latest version of phpjf and found a bug with the function_exists() function.

When it is compiled the function definition looks like this

function_exists: function (_name: function) {


which causes an error.

I don't think its an actual bug in the code, its just in the compilation of the package :)

Thanks again guys
Have a great day

Gravatar
Plefand
1 Aug '11 Permalink

q  

Gravatar
Soivory
20 Jun '11 Permalink

q  

Gravatar
pleabok
17 Jun '11 Permalink

q  

Gravatar
Impatty
12 Jun '11 Permalink

q  

Gravatar
noptets
10 Jun '11 Permalink

q  

Gravatar
Kevin van Zonneveld
30 Dec '08 Permalink

q  @ Brett Zamir: Thanks Brett. I've added the functions. I do have the idea they could be standardized & improved a bit though, but I'll leave that up to the 'future us' :) Let those guys handle it.

Seriously though: I think the class_exists example I've made should return true, but it doesn't. That's a good one to start with.

Gravatar
Brett Zamir
23 Dec '08 Permalink

q  Added one last condition which will find if there is a static members on the class to which an object belongs, as well as some example tests for it as well:

[CODE="Javascript"]myClass2.prototype.instanceMethod = function () {}
myClass2.prototype.mine = 'my member';
var_dump(property_exists(new myClass2, 'mine')); //true
var_dump(property_exists(new myClass2, 'xpto')); //true, as of PHP 5.3.0
var_dump(property_exists(new myClass2, 'bar')); //false
var_dump(property_exists(new myClass2, 'staticProp')); //true, as of PHP 5.3.0
var_dump(property_exists(new myClass2, 'staticMethod')); //false
var_dump(property_exists(new myClass2, 'instanceMethod')); //false
var myclass2 = new myClass2();
myclass2.staticMethod(); // doesn't exist
[/CODE]

[CODE="Javascript"]function property_exists (cls, prop) {
cls = (typeof cls === 'string') ? window[cls] : cls;
if (typeof cls === 'function' && cls.toSource && cls.toSource().match(new RegExp('this\\.'+prop+'\\s'))) { // Hackish and non-standard but can probably detect if setting the property (we don't want to test by instantiating as that may have side-effects)
return true;
}
return (cls[prop] !== undefined && typeof cls[prop] !== 'function') || (cls.prototype !== undefined && cls.prototype[prop] !== undefined && typeof cls.prototype[prop] !== 'function') || cls.constructor && cls.constructor[prop] !== undefined && typeof cls.constructor[prop] !== 'function';
}[/CODE]

Gravatar
Brett Zamir
23 Dec '08 Permalink

q  And here's one for property_exists(), based on PHP doc examples:

[CODE="Javascript"]var myClass = {
mine : {},
xpto : {},
test : function () {
var_dump(property_exists('myClass', 'xpto')); // true
}
}
var_dump(property_exists('myClass', 'mine')); //true
var_dump(property_exists('myClass', 'xpto')); //true
var_dump(property_exists('myClass', 'bar')); //false
var_dump(property_exists('myClass', 'test')); // false (PHP considers methods as distinct from properties)
myClass.test(); // true


function myClass2 () {
this.xpto = 'something';
}
myClass2.staticProp = 'staticValue';
myClass2.staticMethod = function () {
var_dump(property_exists('myClass2', 'xpto')); // true
}
myClass2.prototype.instanceMethod = function () {}
myClass2.prototype.mine = 'my member';

var_dump(property_exists('myClass2', 'mine')); //true
var_dump(property_exists(new myClass2, 'mine')); //true
var_dump(property_exists('myClass2', 'xpto')); //true, as of PHP 5.3.0
var_dump(property_exists('myClass2', 'bar')); //false
var_dump(property_exists('myClass2', 'staticProp')); //true, as of PHP 5.3.0
var_dump(property_exists('myClass2', 'staticMethod')); //false
var_dump(property_exists('myClass2', 'instanceMethod')); //false
myClass2.staticMethod(); // true


function property_exists (cls, prop) {
cls = (typeof cls === 'string') ? window[cls] : cls;
if (typeof cls === 'function' && cls.toSource && cls.toSource().match(new RegExp('this\\.'+prop+'\\s'))) { // Hackish and non-standard but can probably detect if setting the property (we don't want to test by instantiating as that may have side-effects)
return true;
}
return (cls[prop] !== undefined && typeof cls[prop] !== 'function') || (cls.prototype !== undefined && cls.prototype[prop] !== undefined && typeof cls.prototype[prop] !== 'function');
}
[/CODE]

Gravatar
Brett Zamir
23 Dec '08 Permalink

q  Here's a slightly hackish and partially non-standard way to get class_exists(). Of course, any function can be instantiated in JavaScript, but I've attempted to do a hopefully more sophisticated checking to see whether it was designed as one.

[CODE="Javascript"]
function A (z) {this.z=z}
alert(class_exists('A')); // true (constructor sets 'this')
var a = new A('str');
alert(class_exists('a')); // false (objects not classes)

function B () {}
B.c = function () {}; // Add a static method, making it a class
alert(class_exists('B')); // true

function C () {}
C.prototype.z = function () {};
alert(class_exists('C')); // true

function D (b) {}
alert(class_exists('D')); // false (seems like a regular function)

var e = {
E : function (z) {this.z=z;}
}
alert(class_exists('e.E')); // false (the 'this' refers to containing object, not to an instance property)


function class_exists (cls) {
cls = window[cls]; // Note: will prevent inner classes
if (typeof cls !== 'function') {return false;}
for (var i in cls.prototype) {
return true;
}
for (var i in cls) { // If static members exist, then consider a "class"
if (i !== 'prototype') {
return true;
}
}
if (cls.toSource && cls.toSource().match(/this\./)) { // Hackish and non-standard but can probably detect if setting a property (we don't want to test by instantiating as that may have side-effects)
return true;
}
return false;
}[/CODE]

Gravatar
Brett Zamir
23 Dec '08 Permalink

q  Fixing an error and adding more examples:

[CODE="Javascript"]function Directory () {}
Directory.test = function () {}
Directory.prototype.read = function () {}
$directory = new Directory('.');
alert(true === method_exists($directory,'read')); // true
alert(true === method_exists($directory,'write')); // false
alert(true === method_exists('Directory','test')); // true
alert(true === method_exists('Directory','read')); // false
alert(true === method_exists($directory,'test')); // false

function method_exists (obj, method) {
if (typeof obj === 'string') {
return window[obj] && typeof window[obj][method] === 'function'
}
return typeof obj[method] === 'function';
}
[/CODE]

Gravatar
Brett Zamir
20 Dec '08 Permalink

q  Sorry, neglected to see a string was allowable to test for a static method:
[CODE="Javascript"]

function Directory () {}
Directory.test = function () {}
Directory.prototype.read = function () {}
$directory = new Directory('.');
alert(true === method_exists($directory,'read')); // true
alert(true === method_exists($directory,'write')); // false
alert(true === method_exists('Directory','test')); // true
[/CODE]

[CODE="Javascript"]function method_exists (obj, method) {
if (typeof obj === 'string') {
return typeof window[obj][method] === 'function'
}
return typeof obj[method] === 'function';
}[/CODE]

Gravatar
Brett Zamir
20 Dec '08 Permalink

q  Here's another one more related to the function here...

Sample based on PHP manual...

[CODE="Javascript"]function Directory () {}
Directory.prototype.read = function () {}
$directory = new Directory('.');
alert(true === method_exists($directory,'read')); // true
alert(true === method_exists($directory,'write')); // false
[/CODE]


[CODE="Javascript"]function method_exists (obj, method) {
return typeof obj[method] === 'function';
}[/CODE]


Contribute a New function

More functions

In this category

call_user_func
call_user_func_array
create_function
forward_static_call
forward_static_call_array
func_get_arg
func_get_args
func_num_args
» function_exists
get_defined_functions
register_shutdown_function

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !