javascript - Get functions (methods) of a class -
this question has answer here:
- es6 iterate on class methods 4 answers
i have dynamically fetch properties , functions of es6 class. possible?
using for...in loop, loop through properties of class instance:
class foo { constructor() { this.bar = "hi"; } somefunc() { console.log(this.bar); } } var foo = new foo(); (var idx in foo) { console.log(idx); }
output:
bar
this function functions. inherited or not, enumerable or not. functions included.
function getallfuncs(obj) { var props = []; { props = props.concat(object.getownpropertynames(obj)); } while (obj = object.getprototypeof(obj)); return props.sort().filter(function(e, i, arr) { if (e!=arr[i+1] && typeof obj[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__"]
Comments
Post a Comment