given these two classes
class Foo{
f1;
get f2(){
return "a";
}
}
class Bar extends Foo {
b1;
get b2(){
return "a";
}
}
let bar = new Bar();
What code will get me this list of properties from the bar
instance? ['f1', 'f2', 'b1', 'b2']
Here is a Babel sample
Update
This should be part of @Marc C's answer:
Using a decorator I can easily turn a non enumerable property into an enumerable property:
class Bar extends Foo {
@enumerable()
get b2(){
return "a";
}
}
Here is the decorator source:
function enumerable() {
return function(target, key, descriptor) {
if (descriptor) {
descriptor.enumerable = true;
}
};
}
Here is a Babel sample
given these two classes
class Foo{
f1;
get f2(){
return "a";
}
}
class Bar extends Foo {
b1;
get b2(){
return "a";
}
}
let bar = new Bar();
What code will get me this list of properties from the bar
instance? ['f1', 'f2', 'b1', 'b2']
Here is a Babel sample
Update
This should be part of @Marc C's answer:
Using a decorator I can easily turn a non enumerable property into an enumerable property:
class Bar extends Foo {
@enumerable()
get b2(){
return "a";
}
}
Here is the decorator source:
function enumerable() {
return function(target, key, descriptor) {
if (descriptor) {
descriptor.enumerable = true;
}
};
}
Here is a Babel sample
Share Improve this question edited Jun 19, 2016 at 23:29 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Jan 7, 2016 at 20:19 SylvainSylvain 19.3k26 gold badges102 silver badges146 bronze badges 2- This honestly seems like a dup of stackoverflow./questions/31054910/get-functions-of-a-class. There is no reason to make the properties enumerable. – loganfsmyth Commented Jan 7, 2016 at 22:11
- 1 babel: "Python internationalization library with an emphasis on web-based applications. For questions about the JavaScript library, please use [babeljs]." – Felix Kling Commented Jan 8, 2016 at 1:31
2 Answers
Reset to default 7That's not valid syntax for declaring properties in a class. Instead, declare them in the constructor.
class Foo {
constructor() {
this.f1 = undefined;
}
}
Then you can get them using Object.keys
.
Using experimental features in Babel will allow you to declare properties using that syntax but their values must be declared.
class Foo {
f1 = 0;
...
}
As for accessing the getters, getters are non-enumerable by default and can't be accessed using Object.keys
or any similar mechanism. However, you can create enumerable getters using Object.defineProperty
.
Object.defineProperty(bar, 'f2', {
get() {
return "a";
}
});
If you're using experimental ES7 features, you can apply a decorator to the class method and get the same behavior. See this Babel sample.
class Foo {
@enumerable()
get b2() {
return "a";
}
}
function enumerable() {
return function(target, key, descriptor) {
if (descriptor) {
descriptor.enumerable = true;
}
}
}
I feel like this was answered before. You can apply Object.getOwnPropertyNames
to the instance and its prototypes:
function getAllPropertyNames(obj) {
let names = [];
do {
names.push.apply(names, Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj);
} while(obj !== Object.prototype);
return names.filter(name => name !== 'constructor');
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742317695a4421170.html
评论列表(0条)