Object.getOwnPropertyNames в массиве, но включает основные атрибуты - PullRequest
0 голосов
/ 23 марта 2020

Я давно пользуюсь Object.getOwnPropertyNames. Это мой код:

var ObjectAdditions = {
    deepFreeze: function(o) {//code},
    extendToArray: function(object) {//code}
};
var properties = Object.getOwnPropertyNames(ObjectAdditions);

Это то, что properties появилось как:

["deepFreeze", "extendToArray"]

К сожалению, я также ожидал появления таких атрибутов, как «prototype» и «constructor». Как я могу это сделать?

1 Ответ

0 голосов
/ 23 марта 2020

Ваш ObjectAdditions - это простой объект с deepFreeze и extendToArray в качестве свойств. Это не класс.

Object.getOwnPropertyNames с классами выглядит так:

class MyClass {
  // the cunstructer doesn't apper anywhere else than as MyClass() itself. It is only callable with "new".
  constructor() {
    // this will be added as a property to the instance
    this.myInstanceAttribute = null;
  }
  // this will be added as a property to the class
  static myStaticFunction() {}
  // this will be added to the prototype
  myInstanceFunction() {}
}
MyClass.myStaticAttribute = null;

// shows the class prototype and name, the statics and length from the constructor-function (numer of expected parameters) 
console.log('class', Object.getOwnPropertyNames( MyClass ));

// only the added "myInstanceAttribute"
console.log('instance', Object.getOwnPropertyNames( new MyClass() ));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...