Use PHP functions in JavaScript

JavaScript property_exists

Checks if the object or class has a property

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
25
function property_exists (cls, prop) {
    // Checks if the object or class has a property  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/property_exists    // +   original by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: function class_a () {this.prop1 = 'one'};
    // *     example 1: var instance_a = new class_a();
    // *     example 1: property_exists(instance_a, 'prop1');
    // *     returns 1: true    // *     example 2: function class_a () {this.prop1 = 'one'};
    // *     example 2: var instance_a = new class_a();
    // *     example 2: property_exists(instance_a, 'prop2');
    // *     returns 2: false
    cls = (typeof cls === 'string') ? this.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');
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
2
3
function class_a () {this.prop1 = 'one'};
var instance_a = new class_a();
property_exists(instance_a, 'prop1');

Should return

1
true

» Example 2

Running

1
2
3
function class_a () {this.prop1 = 'one'};
var instance_a = new class_a();
property_exists(instance_a, 'prop2');

Should return

1
false

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

No comments yet. Be the first!


Contribute a New function

More functions

In this category

class_alias
class_exists
get_class
get_class_methods
get_class_vars
get_declared_classes
get_object_vars
method_exists
» property_exists

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 !