Reputation: 492
Is it possible to read a java script function arguments and their type information?
For example, i have a class below:
sap.ui.define( ['sap/ui/core/theming/Parameters'],
function(P){
'use strict';
var B=C.extend('sap.m.Button', {
metadata:{
properties:{
text:{
type:'string',
group:'Misc',
defaultValue:null
},
type:{
type:'sap.m.ButtonType',
group:'Appearance',
defaultValue:sap.m.ButtonType.Default
}
}
} );
.....B.prototype.setType=function(t){this.setProperty('type',t);....
......lot of code exposes public setter & getter methods for the above properties.......
} );
Complete code of the class can be accessed from here.
I am able to read the method names from the public methods exposed by the class. But this does not allow me to know the arguments and their type information.
For example: B.prototype.setType is a public method exposed. I can acquire an object of Button class but not getting an idea of how to read its argument type info in run time.
If I want to let the user to add value for "type" property (it is button type), i should know the type of value he can enter.
How can i achieve this?
Thanks
Edit 1:
In my first step, i am using getMetadata() method of sap.m.Button class which is exposed by the framework and this gives me the publicMethodNames but it does not give me information about argument type information. So, i am looking for alternatives.
In java, for this kind of need, we have reflection API. How can we achieve the same in javascript?
Edit 2: A screenshot illustrating the result achieved in an IDE.
Upvotes: 2
Views: 92
Reputation: 546
You can access to function parameters with arguments
object:
var f = function () {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i], typeof arguments[i]);
}
}
f(123, 'test', { a: 1}, function () { alert('x') });
UPD How to get declared arguments number:
var f = function (a, b) {
console.log(arguments);
};
console.log(f.length); //2
f(1, 1, 1, 1); //object with 4 keys
f(1); //object with 1 key
Upvotes: 1