最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the public properties of a class without creating an instance of it? - Stack Overflow

programmeradmin1浏览0评论

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 es 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?

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 es 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?

Share Improve this question edited May 28, 2015 at 23:32 Remo H. Jansen asked May 28, 2015 at 23:27 Remo H. JansenRemo H. Jansen 25.1k12 gold badges73 silver badges99 bronze badges 1
  • 1 "As far as I know, 'no.'" You need to be able to interrogate "the Prototype," and offhand I don't know how you would do that . . . – Mike Robinson Commented May 28, 2015 at 23:30
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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 pile time using the TypeScript language service and then do code generation to assist the runtime (https://github./Microsoft/TypeScript/wiki/Using-the-Language-Service-API).

Neither of these are trivial.

发布评论

评论列表(0)

  1. 暂无评论