Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the public properties of a class without creating an instance of it?

Let's imagine that we have a JavaScript class:

var Person = (function () {
    function Person(name, surname) {
        this.name = name;
        this.surname = surname;
    }
    Person.prototype.saySomething = function (something) {
        return this.name + " " + this.surname + " says: " + something;
    };
    return Person;
})();

I want to iterate its methods and properties. I have no problem with the methods.

  var proto = Person.prototype,
      methods = Object.keys(proto);

  // iterate class methods ["saySomething"]
  for (var i = 0; i < methods.length; i++) {
    // do something...
  }

My problem comes when I want to iterate its properties:

  var proto = Person.prototype,
      targetInstance = new Person(), // this is my problem!
      properties = Object.getOwnPropertyNames(targetInstance),

  // iterate class properties ["name", "surname"]
  for (var i = 0; i < properties.length; i++) {
    // do something...
  }

The only way that I have found is to create an instance and use Object.getOwnPropertyNames. I want to use this code as part of a framework so I will not have control over the classes defined by other developers. I want to avoid the need of creating an instance because if the constructor had some sort of validation like:

function Person(name, surname) {

  if(typeof name === "undefined" || typeof surname === "undefined"){ 
    throw new Error() 
  }

  this.name = name;
  this.surname = surname;
}

I wouldn't be able to use the code above. Do you know if it is possible to get the public properties of a class without creating an instance of it?

like image 836
Remo H. Jansen Avatar asked May 28 '15 23:05

Remo H. Jansen


People also ask

Can I access a class without object?

You can access class even without making any object . We normally make instances to use the methods and the attributes of the class.

Can properties be private in c#?

Properties can be marked as public , private , protected , internal , protected internal , or private protected . These access modifiers define how users of the class can access the property.

When to use properties c#?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they're public data members, but they're special methods called accessors.

What is an instance of a property class?

An instance is a set of properties and property values associated with a property class.


2 Answers

The properties don't exist until an object constructs them. If your class looked like:

var Person = (function () {
    Person.prototype.name = null;    
    Person.prototype.surname = null;
    function Person(name, surname) {
        this.name = name;
        this.surname = surname;
    }
    Person.prototype.saySomething = function (something) {
        return this.name + " " + this.surname + " says: " + something;
    };
    return Person;
})();

you would see name and surname too, but of course you can't count on the objects looking like that.

like image 195
John Hascall Avatar answered Sep 29 '22 17:09

John Hascall


Do you know if it is possible to get the public properties of a class without creating an instance of it?

If you are talking about runtime them no, not without ugly hacks like toString (which gives you a string representation of the function body).

However you can get these at compile time using the TypeScript language service and then do code generation to assist the runtime (https://github.com/Microsoft/TypeScript/wiki/Using-the-Language-Service-API).

Neither of these are trivial.

like image 39
basarat Avatar answered Sep 29 '22 18:09

basarat