Can't list getter / setter properties

I'm working on some reflex code to try and clean up properties and functions, but I can't seem to get the getters / setters at all.

The reflection code I have for the properties:

Reflector = function() { };

Reflector.getProperties = function(obj) {
  var properties = [];
  var proto = obj;
  while (proto != Object.prototype) {
    console.log('Scrapping proto: ', proto);
    for (var prop in proto) {
      console.log('typeof ' + prop + ": ", typeof obj[prop]);
      if (typeof obj[prop] != 'function') {
        properties.push(prop);
      }
    }
    proto = Object.getPrototypeOf(proto);
  }
  return properties;
};

      

And an example of running it (with my debug messages):

var SimpleTestObject = function() {
  this.value = "Test1";
  this._hiddenVal = "Test2";
  this._readOnlyVal = "Test3";
  this._rwVal = "Test4";
};
SimpleTestObject.prototype = {
  get readOnlyVal() {
    return this._readOnlyVal;
  },
  get rwVal() {
    return this._rwVal;
  },
  set rwVal(value) {
    this._rwVal = value;
  },
  func1: function() {
    // Test
  }
};
SimpleTestObject.func2 = function(test) { /* Test */ };
SimpleTestObject.outsideVal = "Test5";

var props = Reflector.getProperties(SimpleTestObject);
console.log('props: ', props);
console.log('Object.getOwnPropertyNames: ', Object.getOwnPropertyNames(SimpleTestObject));
console.log('rwVal property descriptor: ', Object.getOwnPropertyDescriptor(SimpleTestObject, 'rwVal'));
console.log('rwVal (2) property descriptor: ', Object.getOwnPropertyDescriptor(Object.getPrototypeOf(SimpleTestObject), 'rwVal'));

      

What I expect to see as an output for mine Reflection.getProperties(SimpleTestObject)

is this ['readOnlyVal', 'rwVal', 'outsideVal']

, but instead I only see outsideVal

. Also, when I tried to use getOwnPropertyDescriptor()

to see if it was rwVal

enumerable, it returned as undefined. So, after thinking it seemed somehow in the prototype above, I tried to level up and still got undefined.

+3


source to share


1 answer


To enumerate getters, use Object.keys or Object.getOwnPropertiesNames in prototype instead of constructor and / or instance:



function readGetters(obj) {
    var result = [];
    Object.keys(obj).forEach((property) => {
        var descriptor = Object.getOwnPropertyDescriptor(obj, property);
        if (typeof descriptor.get === 'function') {
            result.push(property);
        }
    });
    return result;
}



var SimpleTestObject = function() {
    this.value = "Test1";
    this._hiddenVal = "Test2";
    this._readOnlyVal = "Test3";
    this._rwVal = "Test4";
};
SimpleTestObject.prototype = {
    get readOnlyVal() {
        return this._readOnlyVal;
    },
    get rwVal() {
        return this._rwVal;
    },
    set rwVal(value) {
        this._rwVal = value;
    },
    func1: function() {

    }
};
SimpleTestObject.func2 = function(test) { /* Test */ };
SimpleTestObject.outsideVal = "Test5";


// For constructor
console.log(readGetters(SimpleTestObject.prototype));

// For instance
var instance = new SimpleTestObject();
console.log(readGetters(Object.getPrototypeOf(instance)));
      

Run codeHide result


0


source







All Articles