Es6 Iterate Over Class Methods

ES6 Iterate over class methods

You can use Object.getOwnPropertyNames on the prototype:

Object.getOwnPropertyNames( Animal.prototype )
// [ 'constructor', 'getAnimalType' ]

Iterate through methods and properties of an ES6 class

The constructor and any defined methods are non-enumerable properties of the class's prototype object.

You can therefore get an array of the names (without constructing an instance of the class) with:

Object.getOwnPropertyNames(MyClass.prototype)

You cannot obtain the properties without creating an instance, but having done so you can use the Object.keys function which returns only the enumerable properties of an object:

Object.keys(myInstance)

AFAIK there's no standard way to obtain both the non-enumerable properties from the prototype and the enumerable properties of the instance together.

How to enumerate es6 class methods

Object.keys() iterates only enumerable properties of the object. And ES6 methods are not. You could use something like getOwnPropertyNames(). Also methods are defined on prototype of your object so you'd need Object.getPrototypeOf() to get them. Working example:

for (let name of Object.getOwnPropertyNames(Object.getPrototypeOf(callbacks))) {
let method = callbacks[name];
// Supposedly you'd like to skip constructor
if (!(method instanceof Function) || method === Callbacks) continue;
console.log(method, name);
}

Please notice that if you use Symbols as method keys you'd need to use getOwnPropertySymbols() to iterate over them.

How to iterate over properties of an ES6/2015 class instance

That'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;
}
}
}

How do you iterate over all methods in a JavaScript pseudoclass, regardless of whether or not they are marked enumerable?

You can always use Object.getOwnPropertyNames, which will include non-enumerable properties as well. However, this will not include properties from prototypes, so if you are asking about "pseudoclass instances" you might need to loop the prototype chain with Object.getPrototypeOf.

Get functions (methods) of a class

This function will get all functions. Inherited or not, enumerable or not. All functions are included.

function getAllFuncs(toCheck) {
const props = [];
let obj = toCheck;
do {
props.push(...Object.getOwnPropertyNames(obj));
} while (obj = Object.getPrototypeOf(obj));

return props.sort().filter((e, i, arr) => {
if (e!=arr[i+1] && typeof toCheck[e] == 'function') return true;
});
}

Do test

getAllFuncs([1,3]);

console output:

["constructor", "toString", "toLocaleString", "join", "pop", "push", "concat", "reverse", "shift", "unshift", "slice", "splice", "sort", "filter", "forEach", "some", "every", "map", "indexOf", "lastIndexOf", "reduce", "reduceRight", "entries", "keys", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__", "__lookupGetter__", "__defineSetter__", "__lookupSetter__"]

Note

It doesn't return functions defined via symbols;

How to use an iterator within an ES6 class method

The error is just what it says

for (vertex of this) {

uses a variable vertex that isn't declared anywhere. It should be

for (var vertex of this) {

or let or const.

How to correctly iterate through getElementsByClassName

According to MDN, the way to retrieve an item from a NodeList is:

nodeItem = nodeList.item(index)

Thus:

var slides = document.getElementsByClassName("slide");
for (var i = 0; i < slides.length; i++) {
Distribute(slides.item(i));
}

I haven't tried this myself (the normal for loop has always worked for me), but give it a shot.

ES6 class methods not returning anything inside forEach loop

An arrow function is still a function, and you're only returning from the forEach callback function, not from getTwo, you have to return from the getTwo function as well.

It's not quite clear why you would use a loop to check for something in that way, but the concept would be something like

getTwo() {
var n = 0;
this.nums.forEach(num => {
if (num === 2) n = num;
})
return n; // returns something from getTwo()
}

Getting a list of statics on an ES6 class

Yes, all methods of classes are non-enumerable by default.

You still can iterate them using Object.getOwnPropertyNames. Filter out .prototype, .name and .length (or just everything that is not a function). To include inherited static methods, you will have to walk the prototype chain explicitly (using Object.getPrototypeOf).



Related Topics



Leave a reply



Submit