Feature List in Prototype

I would like to get a list of functions for different JavaScript objects, but in particular String and other primitives. I thought that I could somehow use String.prototype and magically get a list of functions in the prototype, but not a cube. Any ideas?

I also tried using the underscore. for instance

_.functions("somestring");

... does not do this for me.

+4
source share
2 answers

You would use getOwnPropertyNames for this, it returns an array of all properties, enumerated or unrelated

Object.getOwnPropertyNames(String.prototype)

Fiddle

If you only need functions (which could exclude only length, I think?), You can filter

var fns = Object.getOwnPropertyNames(String.prototype).filter(function(itm) {
    return typeof String.prototype[itm] == 'function';
});

Fiddle

+4

, . , Object.getOwnPropertyNames, , .

,

function getFunctions(inputData) {
    var obj = inputData.constructor.prototype;
    return Object.getOwnPropertyNames(obj).filter(function(key) {
        return Object.prototype.toString.call(obj[key]).indexOf("Function") + 1;
    });
}
console.log(getFunctions("Something"));
console.log(getFunctions(1));

, Function, .

+1

Source: https://habr.com/ru/post/1539020/


All Articles