I'd like to be able to get a list of functions on different JavaScript objects, but specifically String and other primitives. I thought I would be able to somehow use String.prototype and magically get a list of the functions in the prototype but no dice. Any ideas?
I also tried using underscore. For example
_.functions("somestring");
...doesn't do it for me.
1) It tells the return type of the data that the function will return. 2) It tells the number of arguments passed to the function. 3) It tells the data types of each of the passed arguments. 4) Also it tells the order in which the arguments are passed to the function.
A function prototype is a definition that is used to perform type checking on function calls when the EGL system code does not have access to the function itself. A function prototype begins with the keyword function, then lists the function name, its parameters (if any), and return value (if any).
A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
Answer: void function ();
You'd use getOwnPropertyNames for that, it returns an array of all properties, enumerable or not
Object.getOwnPropertyNames(String.prototype)
FIDDLE
If you need only the functions (which would only exclude length
I think ?) you could filter
var fns = Object.getOwnPropertyNames(String.prototype).filter(function(itm) {
return typeof String.prototype[itm] == 'function';
});
FIDDLE
The actual problem is, the members of the primitive's prototype are not enumerable. So, we have to use Object.getOwnPropertyNames
, which will give even the non-enumerable properties, to get them.
You can write a utility function, like this
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));
This filters all the properties which are Function objects and returns them as an Array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With