Comment detail

メソッド名一覧の表示 (Nested Flatten)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import types

class A:
    def __init__(self):
        self.bar = 0
        self.test_bar = 1
        self.baz = []
        self.test_baz = {}
    def foo(self):
        print "foo"
    def test_foo(self):
        print "test_foo"
    def boo(self):
        print "boo"
    def test_boo(self):
        print "test_boo"

def call_tests(obj):
    for name in dir(obj):
        if name.startswith("test_"):
            attr = getattr(obj, name)
            if isinstance(attr, types.MethodType):
                attr()

def main():
    call_tests(A())

if __name__ == '__main__':
    main()
上のコードをJavaScriptに移植。IE6とFirefox2で動作確認。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function A()
{
    this.bar = 1;
    this.test_bar = 2;
    this.baz = [];
    this.test_baz = {};
}

A.prototype.foo = function()
{
    alert("foo");
}

A.prototype.test_foo = function()
{
    alert("test_foo");
}

A.prototype.boo = function()
{
    alert("boo");
}

A.prototype.test_boo = function()
{
    alert("test_boo");
}

function call_tests(obj)
{
    for (var name in obj)
    {
        var attr = obj[name];

        if (attr instanceof Function && name.indexOf("test_") == 0)
        {
            attr();
        }
    }
}

call_tests(new A());

Index

Feed

Other

Link

Pathtraq

loading...