JavaScript method_exists function

A JavaScript equivalent of PHP’s method_exists
function method_exists (obj, method) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     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 this.window[obj] && typeof this.window[obj][method] === 'function';
  }

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

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

Should return
true

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

Should return
false
Теги:
method_exists
Добавлено: 25 Июля 2018 19:39:46 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...