Javascript method_exists : Class/Object Information : PHP functions in JavaScript JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » PHP functions in JavaScript » Class/Object Information »

 

Javascript method_exists


Checks if the class method exists
Example 1

Running
1.function class_a() {this.meth1 = function() {return true;}};
2.var instance_a = new class_a();
3.method_exists(instance_a, 'meth1');

Could return
1.true
Example 2

Running
1.function class_a() {this.meth1 = function() {return true;}};
2.var instance_a = new class_a();
3.method_exists(instance_a, 'meth2');

Could return
1.false
function method_exists (obj, method) {
    // Checks if the class method exists  
    // 
    // version: 812.3015
    // discuss at: http://phpjs.org/functions/method_exists
    // +   original by: Brett Zamir
    // *     example 1: function class_a() {this.meth1 = function() {return true;}};
    // *     example 1: var instance_a = new class_a();
    // *     example 1: method_exists(instance_a, 'meth1');
    // *     returns 1: true
    // *     example 2: function class_a() {this.meth1 = function() {return true;}};
    // *     example 2: var instance_a = new class_a();
    // *     example 2: method_exists(instance_a, 'meth2');
    // *     returns 2: false
    if (typeof obj === 'string') {
        return window[obj] && typeof window[obj][method] === 'function'
    }

    return typeof obj[method] === 'function';
}


HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo PHP functions in JavaScript
» Class/Object Information