有没有办法从内部获取 javascript 对象的所有方法(私有、特权或公共)?这是示例对象:
var Test = function() {
// private methods
function testOne() {}
function testTwo() {}
function testThree() {}
// public methods
function getMethods() {
for (i in this) {
alert(i); // shows getMethods, but not private methods
}
}
return { getMethods : getMethods }
}();
// should return ['testOne', 'testTwo', 'testThree', 'getMethods']
Test.getMethods();
当前的问题是 中的代码getMethods()
,简化的示例将仅返回公共方法,而不返回私有方法。
编辑:我的测试代码可能(或可能不会)使我希望得到的东西过于复杂。给出以下内容:
function myFunction() {
var test1 = 1;
var test2 = 2;
var test3 = 3;
}
myFunction()
有没有办法从内部找出存在哪些变量myFunction()
。伪代码如下所示:
function myFunction() {
var test1 = 1;
var test2 = 2;
var test3 = 3;
alert(current.properties); // would be nice to get ['test1', 'test2', 'test3']
}